Object-Oriented Programming For I.T
Object-Oriented Programming For I.T
ITPROG3L
(OBJECT-ORIENTED PROGRAMMING FOR I.T.)
LAB EXERCISE
2
JAVA LANGUAGE FUNDAMENTALS
Cansino
8/27/16
LAB 2: Fundamental of Java
Programming
Learning Outcomes
This Lab sheet encompasses of activities which is 2A, 2B, 2C, 2D, 2E, 2F, 2G, 2H and
2I.
Activity 2A
Activity Outcome: Identify identifies, variables and constants in Java programming
Program DataType.java below illustrates the declaration and initialization of variables of byte,
short, int and long data type:
class DataType
// value to it’s
// 3 value to it’s
// initialized a to it’s
// 3500 to it’s
// 525.9 to it’s
System.out.println(b+","+s+","+i+","+l);
System.out.println(basic+","+hra+","+grade+","+ispermanent);
Procedure:
Step 1 : Type the above program, compile and execute. What is the output?
Output:
Activity 2B
Activity Outcome: Identify identifies, variables and constants in Java programming.
class CircumferenceOfCircle
circumference=2*pi*3;
System.out.println(circumference);
Procedure :
Step 1 : Open Notepad and type the above program, compile and execute. What is the
output?
18.84
Output: 18.84
Activity 2C
Activity Outcome: Implement numeric data types in Java Program.
The following program uses primitive data type of byte and short:
class ShortEg
{
public static void main ( String[] poli )
{
byte value = 127;
System.out.println("Value = " + value);
}
}
Procedures:
Value = 128
Step 5 : Change the value variable value’s to -129 and try to compile and run the
1 error
Step 6 : Change the data type to short . Compile and run the program.
Is there a difference? Explain.
Explanation:
It seems that to print a negative value, one must have the data type on short to prevent erros
c:\Users\bltorres\Documents>java ShortEg
Value = -129
Activity 2D
Activity Outcome: Implement numeric data types in Java Program.
The following program uses primitive data type of float and double:
class DoubleEg
{
public static void main ( String[] doub )
{
float value = 3.4E0.01F;
System.out.println("Value = " + value);
}
}
Procedures:
1 error
Step 2 : Change the value of variable value’s from 3.4E0.38F to 3.4E0.39F.
Output : ----
Step 4 : Change the data type of variable value’s to double. Compile and run the program.
Is there a difference? Explain.
Output : ---
Explanation: No output.
_________________________________________________________________________
Activity Outcome: Implements character and Boolean data types in Java programs.
The following program uses primitive data type of character and boolean :
char letter;
boolean bool;
letter = 'A';
System.out.println(letter);
letter = 'B';
System.out.println(letter);
bool = true;
System.out.println(bool);
bool = false;
System.out.println(bool);
Procedures :
Step 1 : Type the above program.
Output:
B
Activity 2F
true
Activity Outcome: Implements operator precedence in Java programs.
false
System.out.println(6 * 2 / 3);
System.out.println(6 / 2 * 3);
System.out.println(9 + 12 * (8-3));
System.out.println(9 + 12 * 8 - 3);
System.out.println(19 % 3);
System.out.println(5 + 19 % 3 - 1);
Procedures :
69
102
5
Activity 2G
Activity Outcome: Implements typecasting in Java programs.
The following program shows the implicit and explicit type casting.
class TypeWrap {
System.out.println("Variables created");
char c= 'x';
byte b= 50;
short s = 1996;
int i = 32770;
long l= 1234567654321L;
float f1 = 3.142F;
float f2 = 1.2e-5F;
double d2 = 0.000000987;
System.out.println(" " );
System.out.println(" Types converted" );
// floating point
// be integral type
Ouput:
Procedures :
c:\Users\bltorres\Documents> java TypeWrap
Step 1 : Type the above program.
Variables created
Step 2 : Compile and run the program. Observe the output.
c= x
b= 50
s= 1996
i= 32770
l= 1234567654321
f1= 3.142
f2= 1.2E-5
Activity 2H
Activity Outcome: Implements input stream (System.in) and output stream (System.out) in in
Java programs.
The following program InputSample.java show how to accepts input data using input stream,
convert string value to int and display data using output stream.
import java.io.*;
class IntegerInputSample
String str;
System.out.println("Enter an integer:");
str = stdin.readLine();
}
Procedures :
Output:
c:\Users\bltorres\Documents>java IntegerInputSample
Enter an integer:
20
Integer Value: 20
Activity 2I
Activity Outcome: Implements input stream (System.in) and output stream (System.out) in in
Java programs.
class CommandLineInputSample
The following
{ program Program CommandLineInputSample .java below show how to accepts
input from the command line.
public static void main(String args[])
num1=Integer.parseInt(args[0]);
num2=Integer.parseInt(args[1]);
num3=Integer.parseInt(args[2]);
sum=num1+num2+num3;
}
Procedures :
Step 1 : Type the above program.
Step 2 : Compile and run the program. Observe the output.
Output:
c:\Users\bltorres\Documents>java CommandLineInputSample
at CommandLineInputSample.main(CLIS.java:7)
Take a Challenge
1. Write a program that defines and initializes the prices of three items {item1, item2, and item3}
for a store. Your program then adds 5 to each price, prints the new prices and the new total
price to the screen.
2. Design then implement a Java program that defines and initializes two integers a = 18 and b
= 4 then computes and displays the followings:
int quo,rem;
int a=18;
int b=4;
System.out.println("First Integer"+a);
System.out.println("Second Integer"+b);
quo= a/b;
rem= a%b;
3. The user will input a Philippine amount, then the said amount will be converted to different
currencies namely: US Dollar, Euro, Yuan, Koruna, Krone, Sheqel and Dinar
Sample run:
Enter Philippine peso: 43.33089
us = peso*0.021587;
euro = peso*0.019282;
yuan = peso *0.144093;
koruna =peso * 0.520925;
krone = peso *0.143566;
System.out.println("The amount's equivalent to:");
System.out.println("Us Dollar is : "+us);
System.out.println("Euro : "+euro);
System.out.println("Yuan : "+yuan);
System.out.println("Koruna : "+koruna);
System.out.println("Krone : "+krone);
QUESTIONS TO ANSWER:
1. Which of the four arithmetic operators can operate on string as well as numeric operands.
The arithmetic operator than can operate on a string as well as numeric operands is
addition.
2. What is the advantage of using Scanner class over JOptionPane class?
The advantage of using Scanner class over JOptionPane Class is being able to input the
integers faster rather than typing on a single window, and you can preview the inputs
you typed.
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
REFLECTIONS:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________