Professional Documents
Culture Documents
Laboratory Activity #1 Java Fundamentals: Ncp2210 (Computerfundamentalsandprogramming)
Laboratory Activity #1 Java Fundamentals: Ncp2210 (Computerfundamentalsandprogramming)
LABORATORY ACTIVITY #1
JAVA FUNDAMENTALS
NAME: ALMOSURA PAOLO R. DR. JOAN P. LAZARO
SECTION: DATE: GRADE: 2nd yr
The figure below illustrates the concept of a compiled Java program running on Windows,
Linux, Mac, and UNIX computers.
1|Page
NCP 2210 (Computer Fundamentals and Programming)
package example;
/**
* @author Joan Lazaro
*/
// This is a simple Java program.
public class Example {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Programming in Java is fun!");
}
}
Note:
Java is a case-sensitive language. It does not regard uppercase letters as being the same
character as their lowercase equivalents.
All Java programs must be stored in a file with a name that ends with .java.
Comments are ignored by the compiler.
A .java file may contain many classes, but may have only one public class. If a .java file
has a public class, the class must have the same name as the file. For instance, if the file
Pizza.java contains a public class, the class’s name would be Pizza.
Every Java application program must have a method named main.
For every left brace, or opening brace, there must be a corresponding right brace, or
closing brace.
Statements are terminated with semicolons. This does not include comments, class
headers, method headers, or braces.
2|Page
NCP 2210 (Computer Fundamentals and Programming)
Example Program #1
package Xyz;
/**
* @author paolo cutie
*/
// This is a simple Java program.
public class Xyz {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Programming in Java is fun!");
}
}}
Result:
Observation:
The example was good using \n
Example Program #2
package Xyz;
/**
3|Page
NCP 2210 (Computer Fundamentals and Programming)
Observation:
The example program no.2 shows the function of print and println. Using the \n and \t
Identifiers
An identifier is a programmer-defined name that represents some element of a program. Variable
names and class names are examples of identifiers.
The following are some specific rules that must be followed with all identifiers:
The first character must be one of the letters a–z or A–Z, an underscore (_), or a dollar
sign ($).
After the first character, you may use the letters a–z or A–Z, the digits 0–9, underscores
(_), or dollar signs ($).
Uppercase and lowercase characters are distinct. This means itemsOrdered is not the
same as itemsordered.
Identifiers cannot include spaces.
Example Program #3
package Xyz;
/**
5|Page
NCP 2210 (Computer Fundamentals and Programming)
Observation:
The example program no.3 shows how the function of println, int, byte, short and long.
With the use of + and $ sign.
6|Page
NCP 2210 (Computer Fundamentals and Programming)
Example Program #4
package Xyz;
/**
* @author paolo cutie
*/
// This is a simple Java program.
public class Xyz {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double price, tax;
float total;
price = 29.75;
tax = 1.76;
total = 31.51F;
System.out.println("The price of the item is " + price);
System.out.println("The tax is " + tax);
System.out.println("The total is " + total);
}
}
Result:
Observation:
The example program no.4 show how to use the function of println, double and float. With
the use of +sign.
Example Program #5
package Xyz;
/**
* @author paolo cutie
*/
// This is a simple Java program.
public class Xyz {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double distance, mass;
distance = 1.495979E11;
mass = 1.989E30;
7|Page
NCP 2210 (Computer Fundamentals and Programming)
Result:
Observation:
The example program no.5 show the function of double and println. With the use of +sign.
Example Program #6
package Xyz;
/**
* @author paolo cutie
*/
// This is a simple Java program.
public class Xyz {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
boolean bool;
bool = true;
System.out.println(bool);
bool = false;
System.out.println(bool);
}
}
Result:
8|Page
NCP 2210 (Computer Fundamentals and Programming)
Observation:
The example program no.6 shows us how is the function of println and boolean.
Example Program #7
package Xyz;
/**
* @author paolo cutie
*/
// This is a simple Java program.
public class Xyz {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
char letter;
letter = '@';
System.out.println(letter);
letter = '1';
System.out.println(letter);
//using ASCII codes
letter = 65;
System.out.println(letter);
letter = 66;
System.out.println(letter);
}
}
Result:
9|Page
NCP 2210 (Computer Fundamentals and Programming)
Observation:
The example program no. 7 shown the use of println and char. With the use of ASCII codes.
Operators
Arithmetic operators
Operator Meaning Type
+ Addition Binary
− Subtraction Binary
* Multiplication Binary
/ Division Binary
% Modulus Binary
Example Program #8
package Xyz;
/**
* @author paolo cutie
*/
// This is a simple Java program.
public class Xyz {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double regularWages; // The calculated regular wages.
double basePay = 25; // The base pay rate.
double regularHours = 40; // The hours worked less overtime.
double overtimeWages; // Overtime wages
double overtimePay = 37.5; // Overtime pay rate
double overtimeHours = 10; // Overtime hours worked
double totalWages; // Total wages
10 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
Result:
Observation:
The example no.8 shows the funtion of double and println. With the use of +, * and $ sign
and the total wages.
In assignment statements where values of lower-ranked data types are stored in variables
of higher-ranked data types, Java automatically converts the lower-ranked value to the higher-
ranked type. This is called a widening conversion. A narrowing conversion is the conversion of a
11 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
value to a lower-ranked type. Because narrowing conversions can potentially cause a loss of data,
Java does not automatically perform them.
Cast operators
The cast operator lets you manually convert a value, even if it means that a narrowing
conversion will take place. Cast operators are unary operators that appear as a data type name
enclosed in a set of parentheses. The operator precedes the value being converted.
Statement Description
littleNum = (short)bigNum; The cast operator returns the value in bigNum, converted to a short. The converted
value is assigned to the variable littleNum.
x = (long)3.7; The cast operator is applied to the expression 3.7. The operator returns the value
3, which is assigned to the variable x.
number = (int)72.567; The cast operator is applied to the expression 72.567. The operator returns 72,
which is used to initialize the variable number.
value = (float)x; The cast operator returns the value in x, converted to a float. The converted value
is assigned to the variable value.
value = (byte)number; The cast operator returns the value in number, converted to a byte. The converted
value is assigned to the variable value.
Method Description
charAt(index) The argument index is an int value and specifies a character position in the string. The first
character is at position 0, the second character is at position 1, and so forth. The method
returns the character at the specified position. The return value is of the type char.
length() This method returns the number of characters in the string. The return value is of the type
int.
toLowerCase() This method returns a new string that is the lowercase equivalent of the string contained in
the calling object.
toUpperCase() This method returns a new string that is the uppercase equivalent of the string contained in
the calling object.
12 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
Example Program #9
package Xyz;
/**
* @author paolo cutie
*/
// This is a simple Java program.
public class Xyz {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String message = "Java Programming is Fun";
String upper = message.toUpperCase();
String lower = message.toLowerCase();
char letter = message.charAt(2);
int stringSize = message.length();
System.out.println(message);
System.out.println(upper);
System.out.println(lower);
System.out.println(letter);
System.out.println(stringSize);
}
}
Result:
Observation:
Example program no.9 shows the function string, char, int and println.
13 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
Method Description
nextByte Returns input as a byte.
nextDouble Returns input as a double.
nextFloat Returns input as a float.
nextInt Returns input as an int.
nextLine Returns input as a String.
nextLong Returns input as a long.
nextShort Returns input as a short.
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String name; // To hold a name
int hours; // Hours worked
double payRate; // Hourly pay rate
double grossPay; // Gross pay
14 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
Observation:
Example program no.10 shows the function of string, int, double and println. With the use
of the signs of +, * and $.
Dialog Boxes
The JOptionPane class allows you to quickly display a dialog box, which is a small graphical
window displaying a message or requesting input. A dialog box is a small graphical window that
displays a message to the user or requests input. You can quickly display dialog boxes with the
JOptionPane class.
Message Dialog. A dialog box that displays a message; an OK button is also displayed
Input Dialog. A dialog box that prompts the user for input and provides a text field where input
is typed; an OK button and a Cancel button are also displayed
// Display a greeting
15 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
Result:
Observation:
The example program no.11 shows the funtion of string and exit. With the use of null.
Method Description
Byte.parseByte Convert a string to a byte.
Double.parseDouble Convert a string to a double.
Float.parseFloat Convert a string to a float.
Integer.parseInt Convert a string to an int.
Long.parseLong Convert a string to a long.
Short.parseShort Convert a string to a short.
16 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
Result:
Observation:
the last example program shows the function of string. With the use of joptionpane and
input dialog
17 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
18 | P a g e