0% found this document useful (0 votes)
19 views14 pages

Labtask 3

Uploaded by

malikuzair1242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

Labtask 3

Uploaded by

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

COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS

Name Uzair Malik


Class BCS-3B
Reg.no SP23-BCS-065

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

public class Book {


int bookid;
int bookpages;
int price;

public static void main(String[] args) {


Book myBook = new Book();
myBook.bookid = 123;
myBook.bookpages = 200;
myBook.price = 20;
myBook.setBookid(111);
myBook.showBook();
}

public int getBookid() {


return bookid;
}

1|Page

[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS

public int getBookpages() {


return bookpages;
}

public int getPrice() {


return price;
}

public void setBookid(int id) {


this.bookid = id;
}

public void showBook() {


System.out.println("Book ID: " + bookid);
System.out.println("Book Pages: " + bookpages);
System.out.println("Price: $" + price);
}
}

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

public class Fibonacci {


public void generateSeries(int n) {
int a = 1, b = 1;
System.out.print(a + " " + b + " ");
for (int i = 2; i < n; i++) {
int c = a + b;
System.out.print(c + " ");
a = b;

2|Page

[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS

b = c;
}
}

public static void main(String[] args) {


Fibonacci fib = new Fibonacci();
int n = 10; // Change this value to generate Fibonacci series of desired length
fib.generateSeries(n);
}
}

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


int radius;
double pi = 3.14;

public Circle() {
radius = 0;
}

public Circle(int r) {
radius = r;
}

public double circumference() {

3|Page

[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS

double circ = 2 * pi * radius;


return circ;
}

public static void main(String[] args) {

Circle c1 = new Circle();


System.out.println("Circle radius (no argument constructor): " + c1.radius);
System.out.println("Circumference (no argument constructor): " + c1.circumference());

Circle c2 = new Circle(5);


System.out.println("Circle radius (one argument constructor): " + c2.radius);
System.out.println("Circumference (one argument constructor): " + c2.circumference());
}
}

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;

//constructor with 0 parameter


public Account()
{
balance=0;
}

public Account(int initialbal)


{

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

Account acc2 = new Account(500);


System.out.println("Account 2 - Initial balance (one argument constructor): " + acc2.balance);
acc2.deposit(200);
acc2.withdraw(700);
System.out.println("Account 2 - Final balance: " + acc2.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

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license


* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package marks;

/**
*
* @author UZAIR MALIK
*/
public class Marks {
int mark1;
int mark2;
int mark3;

// Constructor with no arguments


public Marks() {
mark1 = 0;
mark2 = 0;
mark3 = 0;
}

// Constructor with three arguments


public Marks(int m1, int m2, int m3) {
mark1 = m1;
mark2 = m2;
mark3 = m3;
}

// Method to calculate and return the sum of marks


public int calculateSum() {
return mark1 + mark2 + mark3;
}

6|Page

[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS

public static void main(String[] args) {


// Creating an instance of Marks with no arguments
Marks marks1 = new Marks();
System.out.println("Sum of marks 1: " + marks1.calculateSum()); // Output: 0

// Creating an instance of Marks with three arguments


Marks marks2 = new Marks(80, 75, 90);
System.out.println("Sum of marks 2: " + marks2.calculateSum()); // Output: 245
}
}

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

private int hour;


private int minute;
private int second;

public Time() {
hour = 0;
minute = 0;
second = 0;
}

public Time(int hour, int minute, int second) {


if (isValidTime(hour, minute, second)) {
this.hour = hour;

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

private boolean isValidTime(int hour, int minute, int second) {


return (hour >= 0 && hour < 24 && minute >= 0 && minute < 60 && second >= 0 && second
< 60);
}

public void display() {


System.out.printf("%02d:%02d:%02d\n", hour, minute, second);
}

public static void main(String[] args) {


Time t1 = new Time();
t1.display();

Time t2 = new Time(12, 34, 56);


t2.display();

Time t3 = new Time(25, 61, 72);


t3.display();
}
}

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;

public class Marks {


private int mark1;
private int mark2;
private int mark3;

public Marks(int mark1, int mark2, int mark3) {


this.mark1 = mark1;
this.mark2 = mark2;
this.mark3 = mark3;
}

public void setMark1(int mark1) {


this.mark1 = mark1;
}

public void setMark2(int mark2) {


this.mark2 = mark2;
}

public void setMark3(int mark3) {


this.mark3 = mark3;
}

public int getMark1() {


return mark1;

9|Page

[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS

public int getMark2() {


return mark2;
}

public int getMark3() {


return mark3;
}

public static void main(String[] args) {


Marks marks = new Marks(75, 85, 90);

System.out.println("Mark 1: " + marks.getMark1());


System.out.println("Mark 2: " + marks.getMark2());
System.out.println("Mark 3: " + marks.getMark3());

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

public class Distance {


private int feet;
private int inches;

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

public static void main(String[] args) {


Distance distance1 = new Distance();
distance1.display();

Distance distance2 = new Distance(5, 6);


distance2.display();
}
}

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

public class Building {


public int floors;
public double area;
public int occupants;

public Building(int floors, double area, int occupants) {


this.floors = floors;
this.area = area;
this.occupants = occupants;
}

public void areaPerPerson() {


double areaPerPerson = area / occupants;
System.out.println("Area per person: " + areaPerPerson + " square
units");
}

public static void main(String[] args) {


Building house = new Building(2, 2000, 4);
Building office = new Building(5, 5000, 20);

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

public class HotDogStand {


private int standID;
private int hotDogsSold;

// Constructor
public HotDogStand(int standID, int hotDogsSold) {
this.standID = standID;
this.hotDogsSold = hotDogsSold;
}

// Method to increment the number of hot dogs sold


public void justSold() {
hotDogsSold++;
}

// Getter method to retrieve the number of hot dogs sold


public int getHotDogsSold() {
return hotDogsSold;

13 | P a g e

[email protected]
COMSATS UIVERSITY ISLAMABAD ,ABBOTTABAD CAMPUS

public static void main(String[] args) {


// Creating three hot dog stands
HotDogStand stand1 = new HotDogStand(1, 0);
HotDogStand stand2 = new HotDogStand(2, 0);
HotDogStand stand3 = new HotDogStand(3, 0);

// Simulating hot dog sales for each stand


stand1.justSold();
stand1.justSold();
stand2.justSold();
stand2.justSold();
stand2.justSold();
stand3.justSold();
stand3.justSold();
stand3.justSold();
stand3.justSold();

// Displaying the number of hot dogs sold for each stand


System.out.println("Hot dogs sold at Stand 1: " + stand1.getHotDogsSold());
System.out.println("Hot dogs sold at Stand 2: " + stand2.getHotDogsSold());
System.out.println("Hot dogs sold at Stand 3: " + stand3.getHotDogsSold());
}
}

14 | P a g e

[email protected]

You might also like