Documente Academic
Documente Profesional
Documente Cultură
June, 2016
Lab Practical 1
Objectives
The main objectives on this java program fist lab practical is to know how we will output
(getting output) from java program in different ways.
Question
Add the given lines to your java application (Greeter), enter one sentence and see what they
print. Then enter all then see the output.
Codes
The following below are the codes that will give a result of above question. First program
System.out.println("2+2");
System.out.println(2+2);
System.out.println(2+2 == 4);
System.out.println("2+2");
System.out.println(2+2);
System.out.println(2+2 == 4);
System.out.println(2 > 3);
int x=17;
System.out.println(x);
System.out.println("x=" + x);
Outputs
Below is the output for first program.
Below is the output for second program.
Conclusion
In the first one lab show on how the java program will give you the output based on what you
expect to. Its command to output is different from other program such as c, c++ etc.
Lab Practical 2
Objectives
In second lab practical, the main aim is to understand the use of method in java program. It show
on how different method can be applied in java program. In this there is method with no
argument, one argument, and multiple argument.
Question
Create Method with no argument, one argument and multiple arguments
Codes (1)
Codes with no argument:
int a,b;
a=5;
b=2;
sum(a,b);
int sum;
sum=a+b;
predictRaised(445.00);}
double newAmount;
newAmount=myAmount*1.10;
System.out.println(newAmount);
Codes (2)
int x,t;
x=1010;
t=2020;
sum(x,t);
int sum;
sum=x+t;
System.out.println(sum);
Codes (3)
String fName="winnie";
String mName="ndondi";
String lName="pakawa";
names(fName,mName,lName);
}
System.out.println("FName is ");
System.out.println(fName);
System.out.println("MName is ");
System.out.println(mName);
System.out.println("lName is ");
System.out.println(lName);
Outputs 1
Outputs 2
Outputs 3
Conclusion
To know on how we can use or apply method in java program is very important and in this lab
we where dealing on three thing method with argument, with one argument, and with multiple
argument.
Lab Practical 3
Objectives
The main objectives of the lab practical 3 is to reduce the repetition (redundancy) of the code ,
Question
1. Type the given Java program (Commission) which has two methods with the same name
computeCommission(). The first method takes two arguments of type double and second
argument call two arguments one is double another is integer. Then compile and run then see
the output.
Codes
First Codes for test3:
Commission.computeCommission(200000.00, 20);
Commission.computeCommission(400000.00, 0.08);
Task:
double result;
commissionRate = rate/100;
Commission.computeCommission(200000.00, 20);
Commission.computeCommission(400000.00, 0.08);
Commission.computeCommission(400000.00);
2.
//first method
doublegrossPay,netPay;
//Second method
doubleholdingRate,grossPay,netPay;
holdingRate= 0.15;
netPay = grossPay-holdingRate;
//third method
doublepayRate,holdingRate,grossPay, netPay;
payRate = 4.65;
holdingRate = 0.15;
Out put
Pay.computeNetPay(10,7.89,0.45);
Pay.computeNetPay(12.00);
Pay.computeNetPay(8.0, 9.10);
Output
Conclusion
The program in lab practical no 3 is to reduce the repetition of codes
Lab Practical 4
Objectives
Demonstration about Inheritance concept Use of package
Question
1 .Type this Java program (Book) which has one methods with the name display(). The method
takes two arguments of type double and int. Then compile it.
Codes
1.
Testing Book.
}
Outputs 1
2.
import java.util.*;
import java.io.*;
char a;
String java;
int pageNumber;
java = neno.readLine();
pageNumber = keyboard.nextInt();
a = (char)System.in.read();
display(java, pageNumber);
grade(a);
Outputs 2
Conclusion
The output above shows the inheritance between a different part of the program combined
together.
Lab Practical 5
Objectives
The objective of the lab practical 5 is the demonstration ofMethod Overriding and Polymophism
Question
1.Type this Java program (Square) which contain data field for height, width, surfaceArea and
method named computeSurfaceArea.
Codes
1.
public class Square{
int height = 5;
int width = 6;
int surfaceArea;
public void computeSurfaceArea(){
surfaceArea = height * width;
System.out.println("The area of square is " + surfaceArea);
}
}
// square
Conclusion
Java also show the program overloading. This means that you can create the super class ,and
from that super class you can create sub classes.