Lab Manuals
Lab Manuals
Noman 42935
Lab No # 1
Task 1.1
Write a program that creates a class called laptop. The data members of the class are brand (string),
model (string), serial (int), color (string), price (float), processor speed (float), RAM (int), screen
size(float). Create member function that will set the individual values. Since the RAM can be upgraded
therefore create a function that allows you to upgrade the RAM only. In the end, create a function that
will display all the data members.
package lab4.pkg1;
class laptop{
brand = b;
model = m;
color = c;
serial = s;
RAM = ram;
price = p;
M. Noman 42935
Pspeed = sp;
Ssize = sz;
void setDisplay(){
System.out.println("Brand "+brand);
System.out.println("Model "+model);
System.out.println("Color "+color);
System.out.println("Serial "+serial);
System.out.println("Price "+price);
System.out.println("Screen size"+Ssize);
void setUpDisplay(){
System.out.println("Ram "+RAM);
ram.setUpgrade(50);
data.setBrand("2914 HP");
M. Noman 42935
data.setModel("123");
data.setColor("Silver");
data.setSerial(128594);
data.setPrice(224.99f);
data.setSpeed(3500.55f);
data.setSize(21.545f);
data.setDisplay();
ram.setUpDisplay();
}}
OUTPUT
Task 1.2
Write a class called rectangle. Your task is to store the length and width of the rectangle. Write
a member function called increment that will add 1 to the value of length and width. Also write
a function that will compute the area of the rectangle. Finally write a constant function that
will display the length, width and area of the rectangle. Demonstrate the use of the object in
the main function. Make sure that the function names are meaningful and self-descriptive.
package angle;
class rectangle{
float area;
height = h + 1;
width = w + 1;
void setArea(){
void setdisplay(){
System.out.println("Width"+width);
}}
angl.setIncreament(10,10);
angl.setArea();
angl.setdisplay();
OUTPUT
Task 1.3
M. Noman 42935
Write a program that creates a class called number. Your class will have two data members namely
num (float) and result (int). To find the factorial of the entered number you will need to design three
functions as follows:
Remember that to find the factorial the number must of positive and a whole number. So if any of these
conditions are not met then you cannot determine the factorial.
package lab4.pkg3;
class number{
if(n%1== 0){
num = (int) n;
else
if(n>=0)
num = (int) n;
void setFactorial(){
float fact=1;
fact =fact * n;
void setDisplay(){
System.out.println("number "+num);
System.out.println("factorial "+result);
}}
ab.setWhole(5);
ab.setFactorial();
ab.setDisplay()
}}
OUTPUT
LAB NO #2
Task 2.1
Write a program that creates a class called CUBE. The data members of the class are length, width
height. Create constructor that will set the individual values. In the end, create a function that will
display all the data members.
package lab5.pkg2;
class cube{
cube(){
length =10;
width = 20;
height = 30;
void display(){
System.out.println("Length = "+length);
System.out.println("Width = "+width);
System.out.println("Height = "+height);
}}
c.display();
}}
OUTPUT
Task 2.2
Write a class called rectangle. Your task is to store the length and width of the rectangle. Create
constructor that will set the individual values taken as input by the user. Finally write a function that
will check if two objects are equal by comparing the length and width, this function will take another
rectangle as parameter. Demonstrate the use of the object in the main function. Make sure that the
function names are meaningful and self-descriptive.
M. Noman 42935
package lab5_3;
import java.util.Scanner;
class rectangle{
rectangle(){
length = 0;
width = 0;
length = s.nextInt();
width = s.nextInt();
length = l;
width = w;
}}
System.out.println("Lenth = "+l.length);
System.out.println("Widht = "+l.width);
}}
OUTPUT
M. Noman 42935
Task 2.3
Write a program that creates a class called Student. Your class will have data members namely name,
cmsid, cgpa, array of marks. You will need to design three functions as follows:
Default Constructor.
Parameterized Constructor.
Function to copy data of one student to another.
Function to display the student record.
Write a mainClass to demonstrate the usage of Student class. Also use array of students and call
package lab5_4;
class student{
student(){
name = "";
cmsid = 0;
cgpa = 0.0f;
name = n;
M. Noman 42935
cmsid = cm;
cgpa = cg;
marks = m;
s1.name = s2.name;
s1.cmsid = s2.cmsid;
s1.cgpa = s2.cgpa;
s1.marks = s2.marks;
void display(){
System.out.println("Name: "+name);
System.out.println("CMSID: "+cmsid);
System.out.println("CGPA: "+cgpa);
}}}
int[] m = {22,23,20,18,21,16};
s.display();
}}
OUTPUT
M. Noman 42935
Lab NO #3
Task 3.1
package constructoroverloading;
class Employee {
int id;
String name;
int Empid;
// Empty constructor
public Employee() {
this.id = 0;
this.id = id;
this.id = id;
this.name = name;
}}
e1.display();
e2.display();
e3.display();
}}
OUTPUT
M. Noman 42935
Task 3.2
Write a program that creates a class BOX.
package box;
public Box(){
this.width = 0;
this.height = 0;
M. Noman 42935
this.depth = 0;
this.height = height;
this.height = height;
this.width = width;
this.width = width;
this.height = height;
this.depth = depth;
this.width = width;
return this.width;
this.height = height;
return this.height;
public void setDepth(int depth){
this.depth = depth;
return this.depth;
@Override
return ("Height: " + getHeight() + "\tWidth: " + getWidth() + "\tDepth: " + getDepth());
System.out.println(b1.toString());
b1 = new Box(22);
System.out.println(b1.toString());
b1 = new Box(18,20);
System.out.println(b1.toString());
b1 = new Box(14,16,18);
System.out.println(b1.toString());
b2.setHeight(12);
b2.setWdith(15);
b2.setDepth(10);
OUTPUT
Lab No #4
Task 4.1
Write a program to display the time taken using StringBuffer, String and StringBuilder types of the
data.
package starttime;
// assign a current time mills with system dot to a long data type startTime
// then make a new String buffer with a string parameter name "java"
// loop starts from 0 to till 99999 executed and then appended Tpoint string characters into sb
string buffer
M. Noman 42935
sb.append("Tpoint");
// display the time whic start time sbutracting from current time mills
startTime = System.currentTimeMillis();
// loop starts from 0 to till 99999 executed and then the sb1 string value adding with Tpoint and
sb1+="Tpoint";
startTime = System.currentTimeMillis();
sb2.append("Tpoint");
"ms");
}}
OUTPUT
M. Noman 42935
Task 4.2
Assume a String object str is assigned as a string value. Write a code to replace all occurrences of
package lab7_2;
String str2 ;
int k = 2, a = 5;
org1.setCharAt(4, 'd');
}}
OUTPUT
M. Noman 42935
Task 4.3
Write a program that reads in a sentence and displays the count of individual vowels in the sentence.
Repeat the operation until an empty string is entered. Count only the lower case vowels.
Sample Output:
Enter sentence:
I like java
# of a=2
# of e=1
# of i=i
# of o=0
# of u=0
package vowels;
import java.util.*;
public int n = 5;
// the scanner type of data type given a property a line.separator to string characters
String name;
char letter;
name = scn.next();
scn.useDelimiter(System.getProperty("line.separator"));
// using loop for calculating the count of vowels and giving condition to find the vowels in a sentence
letter = name.charAt(i);
if (letter == 'a'){
vowel1++;
if (letter == 'e'){
vowel2++;
if (letter == 'i'){
vowel3++;
if (letter == 'o'){
vowel4++;
if (letter == 'u'){
vowel5++;
System.out.println("vowel a = "+vowel1);
System.out.println("vowel e = "+vowel2);
System.out.println("vowel i = "+vowel3);
System.out.println("vowel o = "+vowel4);
System.out.println("vowel u = "+vowel5);
M. Noman 42935
OUTPUT