Question
Q1: 1) The class Date has 2 private integer fields: month and year: public class Date { private int month; //must be in range [1..12]
Q1:
1) The class Date has 2 private integer fields: month and year:
public class Date { private int month; //must be in range [1..12]
private int year;
A) Write a suitable constructor that takes 2 integers x and y (representing month and year) and
saves them in the corresponding fields. If the integer x is not in the range [1..12], the constructor
must throw the java exception ArithmeticException to the caller.
(Hint: Methods of parts B, C and D take only one parameter)
B) Write the method equals which checks that two Date objects are equal or not.
public boolean equals (Date A)
C) Write the boolean method lessThan which checks whether a date is older than another date.
public boolean lessThan (Date A)
D) Write a test class that creates a date object of month and year read from the user. If your
constructor throws an exception, your program should print the following message Invalid date. -------------------------------------- Q2:
a)Create a class called Vehicle that contains fields for the vehicle's maximum speed and number of
wheels. Provide both a no-argument constructor and a two-argument constructor. Provide accessor
(get) and mutator (set) methods for the fields.
b) Create a subclass of Vehicle called Bicycle that contains a field for the number of gears on the
bike. All bikes should have two wheels.
package solutions;
c) Add a toString() method to both the Vehicle class and the Bicycle class. Have Bicycle's
toString() use Vehicle's toString(). Write an application that creates a Vehicle with three wheels and
a top speed of 45 mph (like an ATV) and a Bicycle with ten gears and a top speed of 30 mph.
Display both objects.
d) Declare an array of three Vehicles. Make the first two elements Bicycle objects: a 10-speed and a
3-speed. Make the third element just a Vehicle object. Write a loop that runs through the array,
printing each object.
-----------------------------------
Q3:
a) Create an abstract class named Shape. Provide fields for x and y coordinates (the location of the
shape), as well as color. Add gets and sets as well as appropriate constructors. Add an abstract
method called getArea().
b) Develop two child classes of Shape: Rectangle and Triangle. A rectangle has width and height, a
triangle has base and height. Provide gets and sets for each field and appropriate constructors.
Implement the getArea() method in each subclass.
Hint:
Rectangle: A=w X l
Triangle : A=(a X b)/2
( All in java programing language) pls help
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started