Fiza Oop 5
Fiza Oop 5
Karachi Campus
Submitted By:
Submitted To:
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;
Rectangle class:
package fizalab5;
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;
FIZA KHAN 2
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]
Student class:
package lab5_task2;
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 {
Print class:
package lab5_task3;
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;
Insult class:
package lab5_task4;
FIZA KHAN 6
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]
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;
Greet class:
package lab5_task5;
import java.util.Scanner;
FIZA KHAN 8
15/3/2024 Object Oriented Programming
[Introduction to Static Variables and Methods]
Output:
FIZA KHAN 9