Labtask 3
Labtask 3
Q1
Write a class Book with three data members BookId, Pages and Price. It also contains the follow-ing
member function:
get() method is used to input values show() method is used to display values
set() method is used to set the values of data members using parameters
getPrice() method is used to return the value of price
1|Page
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
Q
Write a program to generate a Fibonacci series by creating fibonacci class. The result become like 1 1
2 3 5 8 and so on
2|Page
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
b = c;
}
}
Create a class circle class with radius as data member. Create two constructors (no argument, and two
arguments) and a method to calculate Circumference.
public Circle() {
radius = 0;
}
public Circle(int r) {
radius = r;
}
3|Page
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
Q
Create a class Account class with balance as data member. Create two constructors (no argument, and
two arguments) and methods to withdraw and deposit balance
public class Account
{
int balance;
4|Page
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
balance=initialbal;
}
public void deposit(int total)
{
total =total + balance;
}
public void withdraw(int total)
{
total =total - balance;
}
public static void main(String[] args)
{
Account acc1=new Account();
acc1.deposit(100);
acc1.withdraw(50);
System.out.println("Account 1 - Final balance: " + acc1.balance);
Q
Write a class Marks with three data members to store three marks. Create two constructors and a
method to calculate and return the sum
/*
5|Page
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
/**
*
* @author UZAIR MALIK
*/
public class Marks {
int mark1;
int mark2;
int mark3;
6|Page
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
Q
Write a class Time with three data members to store hr, min and seconds. Create two constructors and
apply checks to set valid time. Also create display function which displays all data members
public Time() {
hour = 0;
minute = 0;
second = 0;
}
7|Page
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
this.minute = minute;
this.second = second;
} else {
System.out.println("Invalid time. Defaulting to 00:00:00.");
hour = 0;
minute = 0;
second = 0;
}
}
8|Page
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
Create an Encapsulated class Marks with three data members to store three marks. Create set and get
methods for all data members. Test the class in runner
package encapsulation;
9|Page
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
marks.setMark1(80);
marks.setMark2(90);
marks.setMark3(95);
System.out.println("\nUpdated Marks:");
System.out.println("Mark 1: " + marks.getMark1());
System.out.println("Mark 2: " + marks.getMark2());
System.out.println("Mark 3: " + marks.getMark3());
}
}
Q
Create a class ―Distance‖ with two constructors (no argument, and two argument), two data
members (feet and inches). Also create display function which displays all data members.
10 | P a g e
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
// No-argument constructor
public Distance() {
this.feet = 0;
this.inches = 0;
}
// Two-argument constructor
public Distance(int feet, int inches) {
this.feet = feet;
this.inches = inches;
}
// Display function
public void display() {
System.out.println("Feet: " + this.feet + ", Inches: " + this.inches);
}
11 | P a g e
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
Q
Create a class building that has the public member floors,area, and occupants and a method
areaperperson()respectively that display the area per person for building. In the main() meth-od create
two instance of building called house and office and display the area per person by di-vision of
area/occupants
System.out.println("For house:");
12 | P a g e
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
house.areaPerPerson();
System.out.println("For office:");
office.areaPerPerson();
}
}
```
Q
Suppose you operate several hot dog stands distributed throughout town. Define an Encapsulated
class named HotDogStand that has an instance variable for the hot dog stand’s ID number and an
instance variable for how many hot dogs the stand has sold that day
// Constructor
public HotDogStand(int standID, int hotDogsSold) {
this.standID = standID;
this.hotDogsSold = hotDogsSold;
}
13 | P a g e
[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS
14 | P a g e