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

NetBeans Style With Screenshots

The document outlines programming lab tasks completed by Muhammad Ahmed Rashid Paracha, including the implementation of three classes: Box, Student, and Tank, each with multiple constructors and display methods. The Box class manages dimensions, the Student class handles student information and includes a copy constructor, while the Tank class manages capacity with fill and empty methods. The main method demonstrates the functionality of these classes with sample outputs for each task.
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)
10 views7 pages

NetBeans Style With Screenshots

The document outlines programming lab tasks completed by Muhammad Ahmed Rashid Paracha, including the implementation of three classes: Box, Student, and Tank, each with multiple constructors and display methods. The Box class manages dimensions, the Student class handles student information and includes a copy constructor, while the Tank class manages capacity with fill and empty methods. The main method demonstrates the functionality of these classes with sample outputs for each task.
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/ 7

Programming Fundamentals Lab Tasks - Muhammad Ahmed Rashid

Paracha 24K-6040
Author: Muhammad Ahmed Rashid Paracha
Student ID: 24K-6040

TASK 1: Box Class (NetBeans Style)

// Author: Muhammad Ahmed Rashid Paracha


// Student ID: 24K-6040

public class Box {


private int width, height;

// Constructor 1: Both height and width


public Box(int width, int height) {
if (width > 0 && height > 0) {
this.width = width;
this.height = height;
} else {
System.out.println("Width and height must be positive.");
}
}

// Constructor 2: Only height, width from user


public Box(int height) {
this.height = height;
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.print("Enter width: ");
this.width = scanner.nextInt();
}

// Constructor 3: No parameters, both from user


public Box() {
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.print("Enter width: ");
this.width = scanner.nextInt();
System.out.print("Enter height: ");
this.height = scanner.nextInt();
}
public void display() {
System.out.println("Box [Width = " + width + ", Height = " + height + "]");
}
}

TASK 1 Output
Box [Width = 5, Height = 10]

NetBeans Screenshot:

TASK 2: Student Class (NetBeans Style)

// Author: Muhammad Ahmed Rashid Paracha


// Student ID: 24K-6040

public class Student {


private int studentID;
private String name;
private float cgpa;
private String department;

// Constructor 1: All variables


public Student(int studentID, String name, float cgpa, String department) {
this.studentID = studentID;
this.name = name;
this.cgpa = cgpa;
this.department = department;
}

// Constructor 2: Only studentID, others from user


public Student(int studentID) {
this.studentID = studentID;
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.print("Enter name: ");
this.name = scanner.nextLine();
System.out.print("Enter CGPA: ");
this.cgpa = scanner.nextFloat();
scanner.nextLine(); // consume newline
System.out.print("Enter department: ");
this.department = scanner.nextLine();
}

// Constructor 3: Only CGPA, others not initialized


public Student(float cgpa) {
this.cgpa = cgpa;
}

// Copy Constructor
public Student(Student other) {
this.studentID = other.studentID;
this.name = other.name;
this.cgpa = other.cgpa;
this.department = other.department;
}

public void display() {


System.out.println("Student [ID = " + studentID + ", Name = " + name + ", CGPA = " +
cgpa + ", Department = " + department + "]");
}
}
TASK 2 Output

Student [ID = 24, Name = Ahmed, CGPA = 3.8, Department = CS]


Enter name: John
Enter CGPA: 3.6
Enter department: IT
Student [ID = 42, Name = John, CGPA = 3.6, Department = IT]
Student [ID = 0, Name = null, CGPA = 3.5, Department = null]
Student [ID = 24, Name = Ahmed, CGPA = 3.8, Department = CS] (Copy Constructor)

NetBeans Screenshot:

TASK 4: Tank Class (NetBeans Style)

// Author: Muhammad Ahmed Rashid Paracha


// Student ID: 24K-6040

public class Tank {


private int capacity;
public Tank(int capacity) {
this.capacity = capacity;
}

public void fill(int amount) {


capacity += amount;
System.out.println("Tank filled. Current capacity: " + capacity);
}

public void empty() {


capacity = 0;
System.out.println("Tank emptied.");
}

@Override
protected void finalize() throws Throwable {
if (capacity != 0) {
System.out.println("Warning: Tank is not empty on cleanup.");
}
super.finalize();
}
}

TASK 4 Output
Tank filled. Current capacity: 30
Tank emptied.

NetBeans Screenshot:
Main Method (NetBeans Style)

// Author: Muhammad Ahmed Rashid Paracha


// Student ID: 24K-6040

public class LabTasks {


public static void main(String[] args) {
// Task 1 Demonstration
Box box1 = new Box(5, 10);
box1.display();

// Task 2 Demonstration
Student student1 = new Student(24, "Ahmed", 3.8f, "CS");
student1.display();
Student student2 = new Student(42);
student2.display();
Student student3 = new Student(3.5f);
student3.display();
Student studentCopy = new Student(student1);
studentCopy.display();
// Task 4 Demonstration
Tank tank = new Tank(10);
tank.fill(20);
tank.empty();
}
}

NetBeans Screenshot:

You might also like