Sunteți pe pagina 1din 18

SOKOINE UNIVERSITY OF AGRICULTURE – SUA

Centre for Information and Communication Technologies – CICT

Laboratory Practical Report

DIT 0218: Introduction to Object-Oriented Programming

Student name: Lema, Jerome Ronald Reg. No.: DIT/D/2014/0090

Instructor: Mr. Joseph Phillipo

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

public class First{

public static void main(String[]args){

System.out.println("2+2");

System.out.println(2+2);

System.out.println(2+2 == 4);

System.out.println(2 > 3);

System.out.println(2 < 3);

Second program codes with additional features from above.

public class First{

public static void main(String[]args){

System.out.println("2+2");

System.out.println(2+2);

System.out.println(2+2 == 4);
System.out.println(2 > 3);

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:

public class Number1{

public static void main(String[]args){

int a,b;

a=5;

b=2;
sum(a,b);

public static void sum(int a, int b)

int sum;

sum=a+b;

System.out.println("the summation of these number="+sum);

public class DemostrationRaise{

public static void main(String[]args){

System.out.println("demostrating some raises:");

predictRaised(445.00);}

public static void predictRaised (double myAmount){

double newAmount;

newAmount=myAmount*1.10;

System.out.println("with raised salary is");

System.out.println(newAmount);

Codes (2)

Code with one argument:

public class Number1{


public static void main(String[]args){

int x,t;

x=1010;

t=2020;

sum(x,t);

public static void sum(int x, int t)

int sum;

sum=x+t;

System.out.println("the summation of these number=");

System.out.println(sum);

Codes (3)

Codes with multiple argument:

public class Monogram{

public static void main(String[]args){

String fName="winnie";

String mName="ndondi";

String lName="pakawa";

names(fName,mName,lName);
}

public static void names(String fName,String mName,String 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:

public class Test3 {

public static void main(String[]args) {

System.out.println("Test the overloaded method");

Commission.computeCommission(200000.00, 20);

Commission.computeCommission(400000.00, 0.08);

Task:

public class Commission{

public static void computeCommission( double sales, double rate) {

double result;

result = sales * rate;

System.out.println("Your commission is :" + result );

public static void computeCommission( double sales, int rate)

double result, commissionRate;

commissionRate = rate/100;

result = sales * commissionRate;


System.out.println("Your commission is :"+ result );

public static void computeCommission( double sales)

double result, commissionRate=7.5;

result = sales * commissionRate;

System.out.println("Your commission is :"+ result );

The main method:

public class Commission2{

public static void main(String[]args) {

System.out.println("Test the overloaded method");

Commission.computeCommission(200000.00, 20);

Commission.computeCommission(400000.00, 0.08);

Commission.computeCommission(400000.00);

Outputs (Result for test 3)

Result for test 3


Result for main method

2.

public class Pay{

//first method

public static void computeNetPay(double hours, double payRate, double holdingRate) {

doublegrossPay,netPay;

grossPay = hours * payRate;

netPay = grossPay - holdingRate;

System.out.println("Your net pay is :" + netPay);

//Second method

public static void computeNetPay(double hours, double payRate) {

doubleholdingRate,grossPay,netPay;

holdingRate= 0.15;

grossPay = hours * payRate;

netPay = grossPay-holdingRate;

System.out.println("Your net pay is :" + netPay);

//third method

public static void computeNetPay(double hours) {

doublepayRate,holdingRate,grossPay, netPay;
payRate = 4.65;

holdingRate = 0.15;

grossPay= hours * payRate;

netPay = grossPay - holdingRate;

System.out.println("Your net pay is : " + netPay);

Out put

Calling the main method

public class TestPay {

public static void main(String[]args) {

System.out.println("Test the overloaded method from another class Pay");

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.

public class Book {

public static void display(String title, int page){

System.out.println("The book title is: " + title);

System.out.println("Its total number of pages is " + page);

public static void grade(char a) {

System.out.println("The book is grade : " + a);

}
Outputs 1

2.

import java.util.*;

import java.io.*;

public class Textbook extends Book{

public static void main(String[]args)throws Exception {

Scanner keyboard = new Scanner(System.in);

BufferedReader neno = new BufferedReader(new InputStreamReader(System.in));

char a;

String java;

int pageNumber;

System.out.print(" Enter title of the book then press enter ");

java = neno.readLine();

System.out.print(" Enter total number of pages then press Enter ");

pageNumber = keyboard.nextInt();

System.out.print(" Enter book category then press Enter ");

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

public class Cube extends Square{


int depth = 10;
public void computeSurfaceArea(){
surfaceArea = height * width* depth;
System.out.println("The area of square is " + surfaceArea);
}
}
3.

public class DemoSquare{


public static void main(String[]args){
Square mraba = new Square();
Cube mraba1 = new Cube();
mraba.computeSurfaceArea();
mraba1.computeSurfaceArea();
}
}
Codes

// square

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);
}
}
// cube
public class Cube extends Square{
int depth = 10;
public void computeSurfaceArea(){
surfaceArea = height * width* depth;
System.out.println("The area of square is " + surfaceArea);
}
}
// DemoSquare
public class DemoSquare{
public static void main(String[]args){
Square mraba = new Square();
Cube mraba1 = new Cube();
mraba.computeSurfaceArea();
mraba1.computeSurfaceArea();
}
}
Results/Outputs
1

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.

S-ar putea să vă placă și