0% found this document useful (0 votes)
10 views12 pages

Fiza Oop 5

The document outlines a lab report for a course on Object Oriented Programming at Bahria University, detailing various programming tasks related to Java. It includes specific tasks such as calculating the area of a rectangle, displaying student information, and creating methods for printing strings and generating insults based on age. Each task is accompanied by code solutions demonstrating the implementation of static methods and classes.

Uploaded by

fizajamshed4
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 (0 votes)
10 views12 pages

Fiza Oop 5

The document outlines a lab report for a course on Object Oriented Programming at Bahria University, detailing various programming tasks related to Java. It includes specific tasks such as calculating the area of a rectangle, displaying student information, and creating methods for printing strings and generating insults based on age. Each task is accompanied by code solutions demonstrating the implementation of static methods and classes.

Uploaded by

fizajamshed4
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/ 12

Bahria University,

Karachi Campus

Course: CSC-210 - Object Oriented Programming


Term: Spring 2024, Class: BSE- 2(C)

Submitted By:

Fiza Khan 89219


(Name) (Reg. No.)

Submitted To:

Engr. Mahawish/Engr. Saniya Sarim

Signed Remarks: Score:


INDEX
SNO DATE LAB LAB OBJECTIVE SIGN
NO
1 16/2/2024 1 Introduction To Java Programming Language

2 23/2/2024 2 Implementation Of Class and Object In OOP

3 1/3/2024 3 Access Modifiers in Java

4 8/3/2024 4 Constructors in Java


5 15/3/2024 5 Introduction to Static Variables and Methods
Bahria University,
Karachi Campus

LAB EXPERIMENT NO.


5
LIST OF TASKS
TASK NO OBJECTIVE
1 Write a program to calculate area of rectangle by using static method.Use
parameterized constructor to assign width and height to the instance. Use Output
area method which uses the static method to calculate the area.
2 Write a program to display Name, Enrollment Number, University Name and,
Semester of students that are from same university and semester using static
fields and methods. (Hint: first set the university name and semester as follows.
3 Write a static method called printNTimes that takes an integer n and a string (in
that order) as its parameters and prints the string n times.
4 Write a static method called insult that has two paramaters, a String which
represents a person’s name and an integer which represents the persons age. This
method should create and return a String which is a personal insult based on the
value of the argument age that was passed. Use the following age cuttoffs (or
variations of your choosing) for creating your insults.
5 Write a static method called greetMe that greets you. The method should issue a
prompt asking for your name, display a polite (or not so polite) greeting message
and then prompt you to enter your age.

Submitted On:
21/3/2024
(Date: DD/MM/YY)
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]

LAB # 05
Task # 01:
Write a program to calculate area of rectangle by using static method.Use
parameterized constructor to assign width and height to the instance. Use
Output area method which uses the static method to calculate the area.

Solution:
Main class:
package fizalab5;
import java.util.Scanner;

public class FizaLab5 {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
System.out.println("Enter the value of width:");
double w=input.nextDouble();
System.out.println("Enter the value of height:");
double h=input.nextDouble();
Rectangle r1=new Rectangle(w,h);
System.out.println("-----------------------------");
r1.OutputArea();
System.out.println("-----------------------------");

Rectangle class:
package fizalab5;

public class Rectangle {


private static double width;
private static double height;

Rectangle(double width,double height){


this.width=width;
this.height=height;
}
public static double Area(double width,double height){
return width*height;
}
public void OutputArea(){
System.out.println("Width: "+width);
System.out.println("Height: "+height);
System.out.println("Area: "+Area(width,height));

FIZA KHAN 1
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]

}
}

Output:

Task # 02:
Write a program to display Name, Enrollment Number, University Name and,
Semester of students that are from same university and semester using static
fields and methods.(Hint: first set the university name and semester as
follows:

Then use static variable counter to get unique roll numbers as follows:

Solution:
Main class:
package lab5_task2;

public class Lab5_Task2 {

FIZA KHAN 2
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]

public static void main(String[] args) {


System.out.println("_______STUDENT_INFO_______");
Student s1=new Student("Mary",23);
Student s2=new Student("Alex",72);
s1.Display();
System.out.println("--------------------------");
s2.Display();
Student.Setuni("Bahria");
Student.Setsem(2);
System.out.println("\n===== AFTER SETTING =====");
System.out.println("--------------------------");
Student.Setrollno();
s1.Display();
System.out.println("--------------------------");
Student.Setrollno();
s2.Display();
}

Student class:
package lab5_task2;

public class Student {


private String name;
private int en;
private static String uni_name="xyz";
private static int sem=0;
private static int counter;

Student(String name,int en){


this.name=name;
this.en=en;
}
public static void Setuni(String u)
{
uni_name=u;
}
public static void Setsem(int s)
{
sem=s;
}
static int Setrollno(){
counter++;
return counter;
}
public void Display(){
System.out.println("Name: "+name);
System.out.println("Enrollment: "+en);
System.out.println("University name: "+uni_name);
System.out.println("Semester: "+sem);
System.out.println("Roll no: "+counter);
}
}

FIZA KHAN 3
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]

Output:

Task # 03:

Write a static method called printNTimes that takes an integer n and a string
(in that order) as its parameters and prints the string n times. For example,

FIZA KHAN 4
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]

Solution:
Main class:
package lab5_task3;
import java.util.Scanner;
public class Lab5_Task3 {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
System.out.println("Enter the word you want to print:");
String in=input.next();
System.out.println("How many times do you want to print the number?");
int n=input.nextInt();
System.out.println("===============================================");
Print.printNTimes(in, n);
}

Print class:
package lab5_task3;

public class Print {

static void printNTimes(String input,int n){


for(int i=0;i<n;i++){
System.out.println(input);
}
}
}

Output:

FIZA KHAN 5
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]

Task # 04:

Write a static method called insult that has two paramaters, a String which
represents a person’s name and an integer which represents the persons age.
This
method should create and return a String which is a personal insult based on
the value of the argument age that was passed. Use the following age cuttoffs
(or variations of your choosing) for creating your insults:

Solution:
Main class:
package lab5_task4;
import java.util.Scanner;

public class Lab5_Task4 {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
System.out.println("Enter the name:");
String name=input.next();
System.out.println("Enter the age:");
int age=input.nextInt();
String i=Insult.insult(name, age);
System.out.println("-------------------------------------");
System.out.println(i);
}

Insult class:
package lab5_task4;

FIZA KHAN 6
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]

public class Insult {

public static String insult(String name,int age){


String insult="";
if(age>1 && age<=10){
insult="Everyone is sweet";
}
else if(age>=11 && age<=17){
insult="They are dweebs";
}
else if(age>=18 && age<=20){
insult= "They are counting down to legal age";
}
else if(age==21){
insult="Exactly they just made legal age";
}
else if(age>=22 && age<=29){
insult="They are counting down to 30";
}
else if(age>=30 && age<=40){
insult="They are suffering adults";
}
else if (age>=41 && age<=50){
insult="They are miserable adults";
}
else if(age>=50){
insult="You a speechless!";
}
return insult;
}
}

Output:

FIZA KHAN 7
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]

Task # 05:
Write a static method called greetMe that greets you. The method should issue
a prompt asking for your name, display a polite (or not so polite) greeting
message and then prompt you to enter your age.

Solution:
Main class:
package lab5_task5;

public class Lab5_Task5 {

public static void main(String[] args) {


Greet.greetMe();
}

Greet class:
package lab5_task5;
import java.util.Scanner;

public class Greet {

public static void greetMe(){


Scanner input=new Scanner(System.in);
System.out.println("Enter your name:");
String name=input.next();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Hello! Have a good day.");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Enter your age:");
int age=input.nextInt();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Thank you!");
}
}

FIZA KHAN 8
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]

Output:

FIZA KHAN 9

You might also like