Hands-on Programming Exercises — Ch. 9
Rectangle with two data fields: width and height (both double). Add a no-arg constructor that sets both to 1.0, and a parameterized constructor that takes width and height. Add methods getArea() and getPerimeter(). In main, create two rectangles and print their areas and perimeters.Rectangle() { width = 1.0; height = 1.0; }. Parameterized: Rectangle(double width, double height) { this.width = width; this.height = height; }. In main: Rectangle r1 = new Rectangle(); and Rectangle r2 = new Rectangle(4.0, 5.0);.-5.0 or 99.0. That's invalid! By making the field private and using a setter with validation, you ensure only valid values (0.0 to 4.0) are accepted. This is encapsulation in action.Student class with private fields: name (String) and gpa (double). Add a constructor that takes both values. Add getters for both fields, and a setter for gpa that only accepts values between 0.0 and 4.0. In main, create a student, try setting an invalid GPA, and show that it doesn't change.private String name; private double gpa;. Constructor: Student(String name, double gpa) { this.name = name; this.gpa = gpa; }. Setter: public void setGpa(double gpa) { if (gpa >= 0.0 && gpa <= 4.0) this.gpa = gpa; }. In main: Student s = new Student("Ali", 3.5);.Car objects have been created in their system at any point. Each individual car has its own brand and year, but the count should be shared across all cars — it doesn't belong to any single car. This is the perfect use case for a static variable: one copy shared by the entire class.Car class with instance fields brand (String) and year (int), and a static field count initialized to 0. In each constructor, increment count. Add a static method getCount(). In main, create 3 cars and print the count after each creation.String brand; int year; static int count = 0;. Constructor: Car(String brand, int year) { this.brand = brand; this.year = year; count++; }. Static method: public static int getCount() { return count; }. In main: Car c1 = new Car("Toyota", 2022);.BankAccount class with private fields owner (String) and balance (double). Use this in the constructor. Add methods: deposit(double amount) — adds to balance (only if amount > 0), withdraw(double amount) — subtracts from balance (only if amount > 0 and amount <= balance), and getBalance(). Print a message for rejected withdrawals.private String owner; private double balance;. Constructor: BankAccount(String owner, double balance) { this.owner = owner; this.balance = balance; }. Withdraw: public void withdraw(double amount) { if (amount > 0 && amount <= balance) balance -= amount; else System.out.println("Insufficient funds!"); }. In main: BankAccount acc = new BankAccount("Sara", 1000.0);.this, and static members.Product class with private fields: name, price, quantity, and a static productCount. Add a constructor (use this), getters, a sell(int qty) method (reject if not enough stock), a restock(int qty) method, and a toString() method. Create an array of 3 products, perform operations, and display the inventory.Product(String name, double price, int quantity) { this.name = name; this.price = price; this.quantity = quantity; productCount++; }. Sell: public void sell(int qty) { if (qty <= quantity) quantity -= qty; else System.out.println("Not enough stock!"); }. Array: Product[] products = { new Product("Laptop", 999.99, 10), new Product("Mouse", 29.99, 50), new Product("Keyboard", 79.99, 30) };. Use for (Product p : products) System.out.println(p); to print.