Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Assignment(Classes & Objects) Exercise 1 Write a class definition for Date, an object type that contains three integers,year, month and day. This class should

JAVA Assignment(Classes & Objects)

Exercise 1

Write a class definition for Date, an object type that contains three integers,year, month and day. This class should provide two constructors. The first should take no parameters. The second should take parameters named year, month and day, and use them to initialize the instance variables. Write a main method that creates a new Date object named birthday. The new object should contain your birthdate. You can use either constructor.

Exercise 2 A rational number is a number that can be represented as the ratio of two integers. For example, 2/3 is a rational number, and you can think of 7 as a rational number with an implicit 1 in the denominator. For this assignment, you are going to write a class definition for rational numbers.

Create a new program called Rational.java that defines a class named Rational. ARational object should have two integer instance variables to store the numerator and denominator. Write a constructor that takes no arguments and that sets the numerator to 0 and denominator to 1. Write a method called printRational that takes a Rational object as an argument and prints it in some reasonable format. Write a main method that creates a new object with type Rational, sets its instance variables to some values, and prints the object. At this stage, you have a minimal testable program. Test it and, if necessary, debug it. Write a second constructor for your class that takes two arguments and that uses them to initalize the instance variables. Write a method called negate that reverses the sign of a rational number. This method should be a modifier, so it should return void. Add lines to main to test the new method. Write a method called invert that inverts the number by swapping the numerator and denominator. Add lines to main to test the new method. Write a method called toDouble that converts the rational number to a double (floating-point number) and returns the result. This method is a pure function; it does not modify the object. As always, test the new method. Write a modifier named reduce that reduces a rational number to its lowest terms by finding the greatest common divisor (GCD) of the numerator and denominator and dividing through. This method should be a pure function; it should not modify the instance variables of the object on which it is invoked. To find the GCD, see Exercise 1). Write a method called add that takes two Rational numbers as arguments and returns a new Rational object. The return object should contain the sum of the arguments. There are several ways to add fractions. You can use any one you want, but you should make sure that the result of the operation is reduced so that the numerator and denominator have no common divisor (other than 1).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

9th Edition

0135188148, 978-0135188149, 9781642087611

More Books

Students also viewed these Databases questions

Question

4. Who would lead the group?

Answered: 1 week ago