Oops Manual
Oops Manual
CPE122
LAB
MANUAL
CPE122 manual
CPE122 manual
Experiment 1.2.
Correcting errors.
This experiment introduces the compiling process and the manner in which your compiler reports
the errors that it finds. Java compilers pinpoint the line where the error has occurred and also
display a short error message. It is then up to you to play the part of detective, going back into the
source program and searching for the error yourself. Often, one small error can spawn several
misleading messages pointing to false errors.
A common procedure when looking for errors (or "debugging") is to compile the program again
after correcting the first real error that you find. It often happens that one correction will cause
other misleading error messages to disappear.
Step 1. The following document is a simple program in the Java programming language. Using
the editor in your particular software development environment, type the program as it appears
here and save it, for future reference, in a file called First.java . Be careful to include all of the
punctuation marks and the braces.
// This is our first Java program.
class First
{
public static void main(String[] args)
{
System.out.println("WELCOME TO ABHA.");
System.out.println("WELCOME TO KKU.");
}
}
CPE122 manual
Step 2. Retrieve the program from Step 1 and compile it. If the compiler finds errors (which
would be typing errors), correct them and try again. Execute the final, correct program. What
happens?
______________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Step 4. In case you didn't have any typing errors on your first attempt, we'll introduce some
now. Change the word System in the source program to system and try to compile the
altered version. How does your compiler inform you of this error?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Step 4. Correct the error introduced in Step 3, and then remove the semicolon from the end of
the line
System.out.println("WELCOME TO ABHA.");
Try to compile this altered version. How does your compiler respond?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Step 5. Correct the error introduced in Step 4, and then remove the closing brace } at the end of
the program. Try to compile this altered version. How does your compiler respond?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Step 6. Correct the error introduced in Step 5, and then change the spelling of main to maine.
Compile and execute the program. What run-time error message did you get?
_________________________________________________________________________
CPE122 manual
______________________________________________________________________________
_____________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Experiment 1.3
Investigate use print() method.
Step1. Modify the original program in Experiment 1.2 to use the method print() instead of
println(). Compile and execute the program. How does this output differ from the output
of the original program?
System.out.print("WELCOME TO ABHA.");
System.out.print("WELCOME TO KKU.");
Step 2. Make a conclusion: What is the difference between println and print?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Step 3. (Modify the existing program by adding \n inside and at the end of each string. Compile
and execute the program. How does the output differ from the output above? How does it
differ from the output in the original Experiment 1.2?
System.out.print("WELCOME TO ABHA.\n");
System.out.print("WELCOME TO KKU.\n");
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Step 4. Add the following lines to the end of the main method. Compile and execute the
program. How does the output produced by each of these statements differ from the others?
System.out.println("1:Chocolate,\nstrawberry, vanilla?");
System.out.println("2:Chocolate,\nstrawberry,\nvanilla?");
System.out.println("3:Chocolate,\n\nstrawberry,\n\nvanilla?");
______________________________________________________________________________
CPE122 manual
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Step 5. What does the \n character combination mean?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Experiment 1.4
Step 1. Replace the statements in the main method of Experiment 1.3 with the single
statement below. Compile the new program. Record what happens.
System.out.println("Oh, I love to "program" in Java");
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Step 2. Correct the statement in Step 1 so that it looks like the first statement below and add
the second statement. Compile and execute the program. Record the results.
System.out.println("Oh, I love to \"program\" in Java.");
System.out.println("Oh, I love \to \"program\" i\n Java.");
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Step 3. Draw a conclusion: What is the meaning of the backslash mark? What are the meanings
of \" and \t ?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
CPE122 manual
______________________________________________________________________________
Experiment 1.5
Step1. Replace the statements in the main method in Experiment 1.4 with the statements
below. Compile and execute. Record the results.
System.out.println("1:Send money quick!"); // To Mom!
System.out.println("2:Send money quick!// To Mom!");
// To Mom! System.out.println("3:Send money quick!");
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________S
tep 2. Add the following lines to the end of the main method. Compile and record the results.
What rule can you derive about the placement of comments?
System.out.println("4:Send money quick!" // To Mom!);
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Exercise
1.1 Write a program that prints the message
My name is Hector, I am
a vector.
I am the subject of many a
physics lecture!
a. all on one line
b. on two lines
c. on eight lines
d. inside a box drawn with asterisks
CPE122 manual
CPE122 manual
class Variables
{
public static void main(String[] args)
{
int answer;
int number = 2;
char chr='x';
double fvalue=5.6;
String name="MyName";
answer = number + 3;
System.out.println("charcter is "+chr);
System.out.println("floating point number is"+fvalue);
System.out.println("integer number is is "+number);
CPE122 manual
System.out.println("charcter is "+name);
}
}
Exercise 2.2: change the values of variables in experiment 2 and assign your own name to the
string data type.
Exercise2.3: Create two variables for each of the data types used in the program experiment 2 and
display them.
CPE122 manual
Program:
class maxtwo{
public static void main(String arg[]){
int a,b;
a=33;
b=23;
if (a<b)
System.out.println("maximum is b");
if (b<a)
System.out.println("maximum is a");
}
}
Exercise3.1: write a program to find minimum of two numbers using simple if statement.
CPE122 manual
Program:
class maxtwo{
public static void main(String arg[]){
int a,b;
a=33;
b=23;
if (a<b)
System.out.println("maximum is b");
else
System.out.println("maximum is a");
}
}
Exercise4.1: write a program to find minimum of two numbers using if-else statement.
CPE122 manual
Program:
import java.util.Scanner;
class scan
{
public static void main(String args[]){
Scanner sc= new Scanner(System.in) ;
int num1,num2,num3;
System.out.println("enter 1st number");
num1=sc.nextInt();
System.out.println("enter 2nd number");
num2= sc.nextInt();
System.out.println("3rd number");
num3=sc.nextInt();
System.out.println("the numbers are="+num1+", "+num2+","+num3);
}
}
CPE122 manual
Exercise 4.1: write the program to read only two numbers from key board.
Exercise4.2: write the program to read four numbers from key board.
CPE122 manual
System.out.println("max is"+n2);
else System.out.println("max is "+n3);
}
}
Exercise5.1: Write a program to find minimum of three numbers.
CPE122 manual
Exercise 6.1 write a program to find sum of first 10 natural numbers using for loop.
Exercise 7.1 write a program to find sum of first 15 natural numbers using while loop.
CPE122 manual
Experiment 8: Do Loop
When you want to test at the end to see whether something should be repeated, the do..while
statement is the natural choice. In this loop the condition is tested at the bottom of the body of the
statement. At least once the body of the loop is executed even if the test condition is false.
CPE122 manual
Exercise 8.1 write a program to find sum of first 15 natural numbers using do loop.
fib=fib1+fib2;
System.out.println(fib);
fib1=fib2;
fib2=fib;
}
}
}
Exercise9.1: write a program to generate first 10 numbers of Fibonacci series.
CPE122 manual
class numbers{
public static void main(String args[]){
int i,j;
for(i=0;i<5;i++){
for(j=i;j>=0;j--)
System.out.print("\t"+j);
System.out.print("\n");
}
}
}
CPE122 manual
class nloop2{
public static void main(String args[])
{
int i,j;
for(i=5;i>=0;i--)
{
for (j=i;j<5;j++)
System.out.print("\t"+j);
CPE122 manual
System.out.print("\n");
}
}
class numbers{
public static void main(String args[]){
int i,j,k;
for(i=0;i<5;i++){
for(j=i;j<5;j++)
System.out.print("\t"+j);
System.out.print("\n");
for(k=0;k<=i;k++)
CPE122 manual
System.out.print("\t");
}
}
}
class numbers{
public static void main(String args[]){
int i,j,k;
for(i=0;i<5;i++){
for(j=i;j<5;j++)
System.out.print("\t"+j);
System.out.print("\n");
for(k=0;k<=i/2;k++)
System.out.print("\t");
CPE122 manual
}
}
}
CPE122 manual
CPE122 manual
int height;
BedRoom(int x, int y, int z)
{
super(x,y);
height=z;
}
int volume()
{
return(length*breadth*height);
}
}
class Inhertest
{
public static void main(String arg[])
{
BedRoom room1= new BedRoom(14, 12, 10);
int area1=room1.area();
int volume1=room1.volume();
System.out.println("Area="+area1);
System.out.println("volum="+ volume1);
}
}
Exercise 12.1 write a program to create 2 objects of type BedRoom and find their araeas and
volumes for each one.
CPE122 manual
Room(float x)
{
length=breadth=x;
}
float area()
{
CPE122 manual
return(length*breadth);
}
}
class TestOverload{
public static void main(String arg[]){
Room room1= new Room(25, 15);
Room room2= new Room(20);
float area1=room1.area();
float area2=room2.area();
System.out.println("Area1 = "+area1);
System.out.println("Area2 = "+area2);
}
}
Exercise 13.1 add statements in the above program to create two more objects one object with one
parameter and second object with 2 parameters.
CPE122 manual
}
}
CPE122 manual
CPE122 manual
}
}