0% found this document useful (0 votes)
91 views

Object-Oriented Programming For I.T

This document provides instructions and code samples for a lab exercise on Java language fundamentals. The learning outcomes are to identify variables, data types, operators, and input/output streams in Java programs. There are 9 activities covering topics like declaring variables of different data types, using constants, operator precedence, typecasting, and accepting user input. For each activity there is sample code and step-by-step instructions to type, compile, and run the code while observing the output.

Uploaded by

davidco wai
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Object-Oriented Programming For I.T

This document provides instructions and code samples for a lab exercise on Java language fundamentals. The learning outcomes are to identify variables, data types, operators, and input/output streams in Java programs. There are 9 activities covering topics like declaring variables of different data types, using constants, operator precedence, typecasting, and accepting user input. For each activity there is sample code and step-by-step instructions to type, compile, and run the code while observing the output.

Uploaded by

davidco wai
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

INFORMATION TECHNOLOGY EDUCATION DEPARTMENT

ITPROG3L
(OBJECT-ORIENTED PROGRAMMING FOR I.T.)

LAB EXERCISE

2
JAVA LANGUAGE FUNDAMENTALS

Torres, Bryan Dave L.


R22

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.

By the end of this laboratory session, students should be able to:


1. Identify identifies, variables and constants in Java programming
2. Implements numeric data types in Java programs
3. Implements character and Boolean data types, operator precedence in Java
programs.
4. Implements typecasting in Java programs.
5. Implements input stream(System.in) and output stream (System.out) in Java
programs.

Hardware/Software: Computer with JDK latest version.

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

public static void main(String pmas[])

byte b = 100; // declare variable b as byte, initialized 100

// value to it’s

short s = 3; // declare variable s as short, initialized

// 3 value to it’s

int i = 65; // declare variable i as integer,

// initialized 65 value to it’s

long l = 123456789; // declare variable l as long,

// initialized 123456789 value to it’s


char grade = 'A'; // declare variable grade as char,

// initialized a to it’s

float basic = 3500; // declare basic as float, initialized

// 3500 to it’s

double hra = 525.9; // declare hra as double, initialized

// 525.9 to it’s

boolean ispermanent = true; // declare ispermanent as

// boolean, initialized true


// 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.

Program CircumferenceOfCircle.java below illustrates variable and constant declarations:

class CircumferenceOfCircle

public static void main(String ppd[])

int radius=3; // declare variables radius as int,

// initialized value 3 to radius.

final double pi=3.14; // declare constant pi as double,

// initialized value 3.14 to pi.

double circumference; // declare circumference as double.

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:

Step 1 : Type the above program.

Step 2 : Compile and run the program. Observe the output.

Step 3 : Change the value of variable value’s from 127 to 128.

Step 4 : Compile and run the program. Observe the output.

Output: c:\Users\bltorres\Documents>java ShortEg

Value = 128

Step 5 : Change the value variable value’s to -129 and try to compile and run the

program. What happens?


c:\Users\bltorres\Documents\step3.java:5: error: incompatible types: possible lossy conversion
from int to byte byte value = -129;

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:

Step 1 : Type the above program.


Output:
Step 2 : Compile and run the program. Observe the output.
c:\Users\bltorres\Documents\2D.java:5: error: ';' expected

float value = 3.4E0.01F;

1 error
Step 2 : Change the value of variable value’s from 3.4E0.38F to 3.4E0.39F.

It still prints out error.

Output : ----

Explanation: It still prints out error.

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.

_________________________________________________________________________

Step 6 : Change the value of variable value’s from 3.4E0.38F to 1.7e308D.

Step 7 : Compile and run the program. Observe the output.

Output: Value = 1.7E308


Activity 2E

Activity Outcome: Implements character and Boolean data types in Java programs.

The following program uses primitive data type of character and boolean :

//A program for demonstrating boolean variables

public class TrueFalse {

public static void main(String [] ags) {

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.

Step 2 : Compile and run the program. Observe the output.

Output:

c:\Users\bltorres\Documents> java TrueFalse

B
Activity 2F
true
Activity Outcome: Implements operator precedence in Java programs.
false

The following program shows the precedence of the operators.

public class precedence {

public static void main ( String[] pre )

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 :

Step 1 : Type the above program.

Step 2 : Compile and run the program. Observe the output.


Output:

69

102

5
Activity 2G
Activity Outcome: Implements typecasting in Java programs.
The following program shows the implicit and explicit type casting.

class TypeWrap {

public static void main ( String args [])

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(" c= " + c);

System.out.println(" b= " + b);

System.out.println(" s= " + s);

System.out.println(" i= " + i);

System.out.println(" l= " + l);

System.out.println(" f1= " + f1);

System.out.println(" f2= " + f2);

System.out.println(" d2= " + d2);

System.out.println(" " );
System.out.println(" Types converted" );

short s1 = b; // implicit type casting

short s2 = (short) i; //explicit type casting

float n1 = (float) i; // from integer change to

// floating point

int m1 = (int) f1; // from floating point turn to

// be integral type

System.out.println(" short s1 = " + s1);

System.out.println(" short s2 = " + (short)i);

System.out.println(" float n1 = " + n1);

System.out.println(" int m1 = " + m1);

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

public static void main (String[] args) throws IOException

InputStreamReader inStream = new


InputStreamReader(System.in);

BufferedReader stdin = new


BufferedReader(inStream);

String str;

int num; // declare an int variable

System.out.println("Enter an integer:");

str = stdin.readLine();

num = Integer.parseInt(str); // convert str to int

System.out.println("Integer Value: "+num);

}
Procedures :

Step 1 : Type the above program.

Step 2 : Compile and run the program. Observe the output.

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[])

int sum, num1, num2, num3;

num1=Integer.parseInt(args[0]);

num2=Integer.parseInt(args[1]);

num3=Integer.parseInt(args[2]);

sum=num1+num2+num3;

System.out.println("The sum = "+sum);

}
Procedures :
Step 1 : Type the above program.
Step 2 : Compile and run the program. Observe the output.
Output:

c:\Users\bltorres\Documents>java CommandLineInputSample

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

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:

- the quotient of dividing a by b,


- the remainder of dividing a by b, and
- the result in floating point of dividing a by b.
public class Test1 {

public static void main(String[] args) {

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;

System.out.println("Quotient of dividing a by b is "+quo);

System.out.println("Remainder of dividing a by b is "+rem);

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

The amount's equivalent to:


US Dollar is : 1.000
Euro : 0.734719
Yuan : 6.346934
Koruna : 18.77263
Krone : 5.449007
Sheqel : 3.726334
Dinar : 0.274588
import java.util.Scanner;
public class PesoConverter {

public static void main(String[] args) {


double us,euro,yuan,koruna,krone;

System.out.println("Enter Philippine Peso");


Scanner reader = new Scanner(System.in);
double peso = reader.nextInt();

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);

Suppose, 1.000 US Dollar is equivalent to Php. 43.33089, 0.734719Euro, 6.346934


Yuan, 18.77263 Koruna, 5.449007 Krone, 3.726334 Sheqel, and 0.274588 Dinar.

4. Create a program that will convert:


4.1 Degree Fahrenheit to degree Celsius and vice versa.

4.2 Centimeter to Meter, decimeter, kilometer, decameter and millimeter.

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.

3. What is the process called Parsing?

Parsing or syntactic analysis is the process of analysing a string of symbols, either innatural


language or in computer languages, conforming to the rules of a formal grammar. The
term parsing comes from Latin pars(orationis), meaning part (of speech).

4. Why is Parsing a requirement when using JOptionPane class?

____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________

REFLECTIONS:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________

You might also like