Question 1

  • Design a class named Rectangle to represent a rectangle. The class contains:
  • Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.
  • A no-arg constructor that creates a default rectangle.
  • A constructor that creates a rectangle with the specified width and height.
  • A method named getArea() that returns the area of this rectangle.
  • A method named getPerimeter() that returns the perimeter.
  • Write a test program that creates three Rectangle objectsone with the default constructor, one with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle in this order

Question 2

  • Design a class named Stock that contains:
  • A string data field named symbol for the stocks symbol.
  • A string data field named name for the stocks name.
  • A double data field named previousClosingPrice that stores the stock price for the previous day.
  • A double data field named currentPrice that stores the stock price for the current time.
  • A constructor that creates a stock with the specified symbol and name.
  • A method named getChangePercent() that returns the percentage changed from previousClosingPrice to currentPrice.
  • Write a test program that creates a Stock object with the stock symbol ORCL, the name Oracle Corporation, and the previous closing price of 49.18. Set a new current price to 49.47 and display the price-change percentage.

Question 3

  • Design a class named Account that contains:
  • A private int data field named id for the account (default 0).
  • A private double data field named balance for the account (default 0).
  • A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.
  • A private Date data field named dateCreated that stores the date when the account was created. Hint: Use the Date classs from java.util
  • A no-arg constructor that creates a default account.
  • A constructor that creates an account with the specified id and initial balance.
  • The accessor and mutator methods for id, balance, and annualInterestRate.
  • The accessor method for dateCreated.
  • A method named getMonthlyInterestRate() that returns the monthly interest rate.
  • A method named getMonthlyInterest() that returns the monthly interest.
  • A method named withdraw that withdraws a specified amount from the account.
  • A method named deposit that deposits a specified amount to the account.
  • (Hint: The method getMonthlyInterest() is to return monthly interest, not the interest rate. Monthly interest is balance * monthlyInterestRate. monthlyInterestRate is annualInterestRate / 12. Note that annualInterestRate is a percentage, e.g., like 4.5%. You need to divide it by 100.)
  • Write a test program that creates an Account object with an account ID of 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the deposit method to deposit $3,000, and print the balance, the monthly interest, and the date when this account was created.

Question 4

  • Design a class named Fan to represent a fan. The class contains:
  • Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed.
  • A private int data field named speed that specifies the speed of the fan (the default is SLOW).
  • A private boolean data field named on that specifies whether the fan is on (the default is false).
  • A private double data field named radius that specifies the radius of the fan (the default is 5).
  • A string data field named color that specifies the color of the fan (the default is blue).
  • The accessor and mutator methods for all four data fields.
  • A no-arg constructor that creates a default fan.
  • A method named outFan() that returns a string description for the fan. If the fan is on, the method returns the fan speed, color, and radius in one combined string. If the fan is not on, the method returns the fan color and radius along with the string fan is off in one combined string.
  • Write a test program that creates two Fan objects. Assign maximum speed, radius 10, color yellow, and turn it on to the first object. Assign medium speed, radius 5, color blue, and turn it off to the second object. Display the objects by invoking their toString method.

Question 5

  • Design a class named QuadraticEquation for a quadratic equation ax2 + bx + x = 0. The class contains:
  • Private data fields a, b, and c that represent three coefficients.
  • A constructor for the arguments for a, b, and c.
  • Three getter methods for a, b, and c.
  • A method named getDiscriminant() that returns the discriminant, which is
    b2 - 4ac.
  • The methods named getRoot1() and getRoot2() for returning two roots of the equation see image.
  • These methods are useful only if the discriminant is nonnegative. Let these methods return 0 if the discriminant is negative.
  • Write a test program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display the two roots. If the discriminant is 0, display the one root. Otherwise, display "The equation has no roots."

Question 6

  • Define a class method (static) named compare to the Fraction class. The compare method accepts two Fraction objects f1 and f2. The method returns
    1 if f1 is less than f2
    0 if f1 is equal to f2
    +1 if f1 is greater than f2
  • Rewrite the compare method above by changing it to an instance method. This method accepts a Fraction object and compares it to the receiving object. The method is declared as follows:
public int compare(Fraction frac) {
//compare the Fraction objects this and frac
//return the result of comparison
}

Question 7

  • In Qn 3 you defined the Account class to model a bank account.
  • An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds.
  • Create two subclasses for checking and saving accounts.
  • A checking account has an overdraft limit, but a savings account cannot be overdrawn.
  • Implement the classes.
  • Write a test program that creates objects of Account, SavingsAccount, and
  • CheckingAccount and invokes their toString() methods.

Question 8

  • Design a class named Person and its two subclasses named Student and Employee.
  • Make Faculty and Staff subclasses of Employee.
  • A person has a name, address, phone number, and email address.
  • A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant.
  • An employee has an office, and salary.
  • A faculty member has office hours and a rank.
  • A staff member has a title.
  • Override the toString method in each class to display the class name and the persons name.
  • Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods.

Question 9

  • Create the GeometricObject class as per the UML diagram shown below see image.

Question 10

  • Create the Circle and Rectangle class that inherit from the GeometricObject Class as per the UML diagram shown see image.

Question 11

  • Write a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.

Question 12

  • Write a program that meets the following requirements:
  • Creates an array with 100 randomly chosen integers.
  • Prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message Out of Bounds.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.