Created
May 29, 2023 02:50
-
-
Save L8RFN/0b2a1444cc71a929f756f4719faa2008 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//q1 | |
Question#1 (Constructors): | |
1. Create a class called Rectangle that contains the following data fields: | |
• height as integer. | |
• width as integer. | |
2. The class also contains the following methods: | |
• A no-arguments constructor that initializes Rectangle data fields to their default values. | |
• A two-arguments constructor that initializes Rectangle data fields to specific given values. | |
• A method named getArea() that returns the area of the rectangle. | |
3. In the main method: | |
• Create an object (r1) with its default values | |
• Create an object (r2) of the Rectangle class with width=10 and height =20. | |
• Print the area of the rectangle r1 by invoking getArea(). | |
public class Rectangle { | |
private int height; | |
private int width; | |
// No-arguments constructor | |
public Rectangle() { | |
height = 0; | |
width = 0; | |
} | |
// Two-arguments constructor | |
public Rectangle(int width, int height) { | |
this.width = width; | |
this.height = height; | |
} | |
public int getArea() { | |
return width * height; | |
} | |
public static void main(String[] args) { | |
// Create an object with default values | |
Rectangle r1 = new Rectangle(); | |
// Create an object with specific values | |
Rectangle r2 = new Rectangle(10, 20); | |
// Print the area of r1 | |
System.out.println("Area of r1: " + r1.getArea()); | |
} | |
} | |
question 2 | |
Question#2 (Access Modifies, Mutators and Accessors): | |
1. Create a class called Student that contains the following data fields: | |
• name as private String. | |
• stdno as private integer. | |
• grade as private integer. | |
2. The class also contains the following methods: | |
• A no-argument constructor to initialize the Student data fields to its default values. | |
• Set and get methods for all data fields. | |
o setGrade method will check if the grade is less than 40 it will be set to 40, | |
otherwise the grade will be the same. | |
• classifyGrade method that has no argument and returns the appropriate letter for the | |
grade according to the following: | |
80 - 100 --------- A | |
50 – 79 ---------- B | |
< 50 -------------- F | |
• print method that prints the Student data fields and the grade letter (invoke classifyGrade | |
method). | |
3. In the main method : | |
• Create an object (s1) of the Student class with name ="Ahmad", ID= 123415, grade=85 | |
• Invoke classifyGrade method and print the corresponding grade letter for s1. | |
• Print all student information. | |
public class Student { | |
private String name; | |
private int stdno; | |
private int grade; | |
// No-arguments constructor | |
public Student() { | |
name = ""; | |
stdno = 0; | |
grade = 0; | |
} | |
// Set methods | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setStdNo(int stdno) { | |
this.stdno = stdno; | |
} | |
public void setGrade(int grade) { | |
if (grade < 40) { | |
this.grade = 40; | |
} else { | |
this.grade = grade; | |
} | |
} | |
// Get methods | |
public String getName() { | |
return name; | |
} | |
public int getStdNo() { | |
return stdno; | |
} | |
public int getGrade() { | |
return grade; | |
} | |
// Classify the grade and return the appropriate letter | |
public String classifyGrade() { | |
if (grade >= 80 && grade <= 100) { | |
return "A"; | |
} else if (grade >= 50 && grade <= 79) { | |
return "B"; | |
} else { | |
return "F"; | |
} | |
} | |
// Print student information | |
public void print() { | |
System.out.println("Name: " + name); | |
System.out.println("Student Number: " + stdno); | |
System.out.println("Grade: " + grade); | |
System.out.println("Grade Letter: " + classifyGrade()); | |
} | |
public static void main(String[] args) { | |
// Create an object of the Student class with specific values | |
Student s1 = new Student(); | |
s1.setName("Ahmad"); | |
s1.setStdNo(123415); | |
s1.setGrade(85); | |
// Print the grade letter for s1 | |
System.out.println("Grade Letter for s1: " + s1.classifyGrade()); | |
// Print all student information | |
s1.print(); | |
} | |
} | |
Question#3 (Instance variables and static variables): | |
1. Create a class called Account that contains the following data fields: | |
• num as a static private integer | |
• id as private integer. | |
• balance as private double. | |
2. The class also contains the following methods: | |
• A no-arguments constructor that | |
o initializes data fields to their default values | |
o increments num and assigns the new num to the id. | |
• The set and get methods for id and balance data fields. | |
• Static set and get method for num data field. | |
• A method called withdraw(double) that withdraws a specified amount from the account | |
balance. Before withdraw, the method should check whether the available balance is | |
sufficient. | |
• A method named deposit(double) that deposits a specified amount to the account | |
balance. | |
3. In the main method : | |
• Create an object (a1) of the Account class with balance=1000. | |
• Create an object (a2) of the Account class with balance=2000. | |
• Print id and balance of a1 and a2. | |
• Withdraw 100 from a1 balance. | |
• Deposit 50 to a1 balance. | |
• Print the balance of a1 and a2. | |
• Print the total number of the created accounts | |
public class Account { | |
private static int num; | |
private int id; | |
private double balance; | |
// Static set and get methods for num | |
public static void setNum(int num) { | |
Account.num = num; | |
} | |
public static int getNum() { | |
return num; | |
} | |
// No-arguments constructor | |
public Account() { | |
id = ++num; | |
balance = 0.0; | |
} | |
// Set and get methods for id and balance | |
public void setId(int id) { | |
this.id = id; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setBalance(double balance) { | |
this.balance = balance; | |
} | |
public double getBalance() { | |
return balance; | |
} | |
// Method to withdraw from the account balance | |
public void withdraw(double amount) { | |
if (balance >= amount) { | |
balance -= amount; | |
System.out.println("Withdrawn: " + amount); | |
} else { | |
System.out.println("Insufficient balance"); | |
} | |
} | |
// Method to deposit into the account balance | |
public void deposit(double amount) { | |
balance += amount; | |
System.out.println("Deposited: " + amount); | |
} | |
public static void main(String[] args) { | |
// Create objects of the Account class | |
Account a1 = new Account(); | |
a1.setBalance(1000); | |
Account a2 = new Account(); | |
a2.setBalance(2000); | |
// Print id and balance of a1 and a2 | |
System.out.println("a1 - ID: " + a1.getId() + ", Balance: " + a1.getBalance()); | |
System.out.println("a2 - ID: " + a2.getId() + ", Balance: " + a2.getBalance()); | |
// Withdraw 100 from a1 balance | |
a1.withdraw(100); | |
// Deposit 50 to a1 balance | |
a1.deposit(50); | |
// Print the balance of a1 and a2 | |
System.out.println("a1 - Balance: " + a1.getBalance()); | |
System.out.println("a2 - Balance: " + a2.getBalance()); | |
// Print the total number of created accounts | |
System.out.println("Total number of accounts: " + Account.getNum()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment