0% found this document useful (1 vote)
425 views

Lab Manuals

The document contains instructions for three programming tasks: 1. The first task involves creating a Laptop class with data members for brand, model, etc. and methods to set/get values and display them. 2. The second task involves creating a Rectangle class with length, width data members and methods to increment size, calculate area, and display values. 3. The third task involves creating a Number class to find the factorial of a number by checking if it is whole and positive, calculating the factorial, and displaying the number and result.

Uploaded by

fari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
425 views

Lab Manuals

The document contains instructions for three programming tasks: 1. The first task involves creating a Laptop class with data members for brand, model, etc. and methods to set/get values and display them. 2. The second task involves creating a Rectangle class with length, width data members and methods to increment size, calculate area, and display values. 3. The third task involves creating a Number class to find the factorial of a number by checking if it is whole and positive, calculating the factorial, and displaying the number and result.

Uploaded by

fari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

M.

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{

String brand, model, color;

int serial, RAM;

float price, Pspeed, Ssize;

void setBrand(String b){

brand = b;

void setModel(String m){

model = m;

void setColor(String c){

color = c;

void setSerial(int s){

serial = s;

void setUpgrade(int ram){

RAM = ram;

void setPrice(float p){

price = p;
M. Noman 42935

void setSpeed(float sp){

Pspeed = sp;

void setSize(float sz){

Ssize = sz;

public void getbrand(){

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("Processor speed "+Pspeed);

System.out.println("Screen size"+Ssize);

void setUpDisplay(){

System.out.println("Ram "+RAM);

public class Lab41 {

public static void main(String[] args) {

// TODO code application logic here

laptop ram = new laptop();

laptop data = new laptop();

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 height=0, width=0;

float area;

void setIncreament(float h, float w){


M. Noman 42935

height = h + 1;

width = w + 1;

void setArea(){

area = height * width;

void setdisplay(){

System.out.println("Height " +height);

System.out.println("Width"+width);

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

}}

public class Angle {

public static void main(String[] args) {

// TODO code application logic here

rectangle angl = new rectangle();

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:

 Function to determine if a number is a whole number or not


 Function to determine if the number is positive or not
 Function to find the actual factorial
 Function to display the number and its factorial

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{

private float num;

private int result;

void setWhole(float n){

if(n%1== 0){

num = (int) n;

else

System.out.println(“The number is not whole number try again “);

void setPositive(float n){

if(n>=0)

num = (int) n;

void setFactorial(){

float fact=1;

for(int n=1; n<=num; n++){

fact =fact * n;

result = (int) fact;


M. Noman 42935

void setDisplay(){

System.out.println("number "+num);

System.out.println("factorial "+result);

}}

public class Lab43 {

public static void main(String[] args) {

// TODO code application logic here

number ab = new number();

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{

int length, width, height;


M. Noman 42935

cube(){

length =10;

width = 20;

height = 30;

void display(){

System.out.println("Length = "+length);

System.out.println("Width = "+width);

System.out.println("Height = "+height);

}}

public class Lab52 {

public static void main(String[] args) {

// TODO code application logic here

cube c = new cube();

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{

int length, width;

rectangle(){

length = 0;

width = 0;

Scanner s = new Scanner(System.in);

System.out.println("Enter a length: ");

length = s.nextInt();

System.out.println("Enter a width: ");

width = s.nextInt();

rectangle(int l, int w){

length = l;

width = w;

}}

public class Lab5_3 {

public static void main(String[] args) {

rectangle l = new rectangle();

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

respective methods of student class.

package lab5_4;

class student{

private String name;

Private int cmsid;

private float cgpa;

private int[] marks;

student(){

name = "";

cmsid = 0;

cgpa = 0.0f;

marks = new int[6];

student(String n, int cm, float cg, int[] m){

name = n;
M. Noman 42935

cmsid = cm;

cgpa = cg;
marks = m;

void copy(student s1, student s2){

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

for (int i = 0; i < marks.length; i++) {

System.out.println("Marks "+ i + " : "+marks[i]);

}}}

public class Lab5_4 {

public static void main(String[] args) {

int[] m = {22,23,20,18,21,16};

student s = new student("M. Noman", 42935, 2.5f, m);

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.name = "Not Available";

// Overloaded constructor with int parameter

public Employee(int id) {

this.id = id;

this.name = "Not Available";

// Overloaded constructor with a int parameter and a string

public Employee(int id, String name) {


M. Noman 42935

this.id = id;

this.name = name;

public void display() {

System.out.println("Employee Info: ");

System.out.println("ID: " + this.id);

System.out.println("Name: " + this.name);

}}

public class ConstructorOverloading {

public static void main(String[] args) {

// Call empty constructor

Employee e1 = new Employee();

e1.display();

// Call one parameter constructor

Employee e2 = new Employee(123);

e2.display();

// Call two parameter constructor

Employee e3 = new Employee(123, "John");

e3.display();

}}

OUTPUT
M. Noman 42935

Task 3.2
Write a program that creates a class BOX.

 Its data members include:


o width, height, depth.
 Class should contain all the:
o getter/setters
o default constructor,
o constructor with height as a parameter
o constructor with height and width as parameters
o constructor with all the parameters.
o function to calculate volume of the box.
o Function to print all the data members.
o Copy constructor.
o Main function to manipulate all the functions and constructors of the class.

package box;

public class Box {

private int width;

private int height;

private int depth;

public Box(){

this.width = 0;

this.height = 0;
M. Noman 42935

this.depth = 0;

public Box(int height){

this.height = height;

public Box(int height, int width){

this.height = height;

this.width = width;

public Box(int width, int height, int depth){

this.width = width;

this.height = height;

this.depth = depth;

public void setWdith(int width){

this.width = width;

public int getWidth(){

return this.width;

public void setHeight(int height){

this.height = height;

public int getHeight(){

return this.height;
public void setDepth(int depth){

this.depth = depth;

public int getDepth(){


M. Noman 42935

return this.depth;

public int volume(){

return (this.width * this.height * this.depth );

@Override

public String toString(){

return ("Height: " + getHeight() + "\tWidth: " + getWidth() + "\tDepth: " + getDepth());

public static void main(String[] args) {

// call empty constructor

Box b1 = new Box();

System.out.println(b1.toString());

// call one parameter constructor

b1 = new Box(22);

System.out.println(b1.toString());

// call two parameters constuctor

b1 = new Box(18,20);

System.out.println(b1.toString());

// three parameter constructor

b1 = new Box(14,16,18);

System.out.println(b1.toString());

System.out.println("Box 1 volme: "+b1.volume());

// call values as a set methods

Box b2 = new Box();

b2.setHeight(12);

b2.setWdith(15);

b2.setDepth(10);

System.out.println("\nBox 2 values\n" + b2.toString());


M. Noman 42935

System.out.println("Box 2 volme: "+b2.volume());


}}

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;

public class StartTime {

public static void main(String[] args) {

// assign a current time mills with system dot to a long data type startTime

long startTime = System.currentTimeMillis();

// then make a new String buffer with a string parameter name "java"

StringBuffer sb = new StringBuffer("Java");

// loop starts from 0 to till 99999 executed and then appended Tpoint string characters into sb

string buffer
M. Noman 42935

for (int i=0; i<10000; i++){

sb.append("Tpoint");

// display the time whic start time sbutracting from current time mills

System.out.println("Time taken by StringBuffer: " + (System.currentTimeMillis() - startTime) + "ms");

startTime = System.currentTimeMillis();

// taking another simple string type new string name as java

String sb1 = new String("Java");

// loop starts from 0 to till 99999 executed and then the sb1 string value adding with Tpoint and

then assingning to sb1

for (int i=0; i<10000; i++){

sb1+="Tpoint";

System.out.println("Time taken by String: " + (System.currentTimeMillis() - startTime) + "ms");

startTime = System.currentTimeMillis();

StringBuilder sb2 = new StringBuilder("Java");

for (int i=0; i<10000; i++){

sb2.append("Tpoint");

System.out.println("Time taken by StringBuilder: " + (System.currentTimeMillis() - startTime) +

"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

lowercase vowels to the letter C by using string and stringBuffer objects.

package lab7_2;

public class Lab7_2 {

public static void main(String[] args) {

String str1 = " I like java programming";

String str2 ;

str2 = str1.replaceAll("[aeiou]", "C");

System.out.println("Original Statement: "+str1);

System.out.println("Replace C on vowels: "+str2);

StringBuffer org1, org2 ;

int k = 2, a = 5;

org1 = new StringBuffer("Quetta Pakistan");

org1.setCharAt(4, 'd');

System.out.println("Original sentence: "+org1);

System.out.println("After replace C on vowels: "+org1);

}}

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 class Vowels {

public int n = 5;

public static void main(String[] args) {

Scanner scn = new Scanner(System.in);

// the scanner type of data type given a property a line.separator to string characters

String name;

int vowel1 = 0, vowel2 = 0, vowel3 = 0, vowel4 = 0, vowel5 = 0;


M. Noman 42935

char letter;

// using input from user to give string data

System.out.println("Enter a string data");

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

for ( int i = 0; i < name.length(); i++){

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy