Write a program to construct and manipulate length objects

Assignment Help Operating System
Reference no: EM13943429

Objectives

• Construct and use Objects

• Design the public interface of a class

• Determine the instance variables necessary for a class

• Implement stubs with comments

• Implement constructors

• Implement simple methods using conditionals

• Access instance fields and local variables

Hand-in Requirements

The project folder should include the following files:

• Length.xml

• Length.java

• LengthTest.java

• the public interface as a text or Word file

Overview

write a program to construct and manipulate Length objects that can handle any of the following units: meters, inches, feet, yards, miles. In addition to modifying the methods, you will include methods for returning the number of meters, for adding two Length objects, and testing whether two Length objects are approximately equal.
Writing the New and Improved Length Class
Constants:
The following constants should be included in the Length class before the instance fields. These constants should be used by your methods. Do not use the numbers in your methods instead of the named constants.
public static final double INCHES_PER_YARD = 36;
public static final double FEET_PER_YARD = 3;
public static final double METERS_PER_YARD = 0.9144;
public static final double YARDS_PER_MILE = 1760;
public static final double EPSILON = 0.000001;

Instance fields:
myNumber - a double value representing the number of the measurement
myUnit - a String value representing the unit of the measurement
Constructors:
/* The constructor has two parameters: */
/* the number and the unit of the measurement. */
public Length(double number, String unit)

Methods:Three methods will be added, and the behavior of some methods will be changed.
The following methods should be added. The comments contain some hints.

/* getMeters returns the length in meters.
This should be implemented to calculate the appropriate conversion
based on the value of myUnit, e.g.,

if ( myUnit.equals("yards") )
meters = myNumber * METERS_PER_YARD;

If myUnit cannot be recognized, assume the unit is meters.
*/

public double getMeters( )
/* add returns a Length that is the sum of two Lengths, e.g.,
if length1 and length2 are two Length objects, then
length1.add(length2) will return a new Length object.

This can be implemented by using the getMeters method,
e.g., this.getMeters() + other.getMeters(), then returning
new Length(sum, "meters")
*/

public Length add(Length other)
/* aboutEqual returns true if the two lengths are within EPSILON
meters of each other.

First, get this.getMeters() and other.getMeters(). Then, return
true if they are close to each other (within EPSILON).
*/
public boolean aboutEqual(Length other)

The getInches, getFeet, getYards, and getMiles methods should be modified. First get the value of the getMeters method, e.g., this.getMeters(), then convert the number of meters to the desired unit using the named constants, and finally return that value.

Testing your Length class:

1. Ask the user to enter a number as a double from the keyboard. Store the response in number2.

2. Ask the user to enter a unit as a String from the keyboard. Store the response in unit2.

3. If unit2 is not "meters", "inches", "feet", "yards", or "miles", then print a warning message and set unit2 to "meters".

4. Instantiate a Length object named length2 using the input from above, and print length2 using the toString method. Be sure to use a descriptive label.

5. Print the values from calling length2 with the getMeters, getInches, getFeet, getYards, and getMiles methods. Be sure to use descriptive labels.

6. Ask the user to enter another number and another unit from the keyboard. Store the responses in number3 and unit3.

7. If unit3 is not "meters", "inches", "feet", "yards", or "miles", then print a warning message and set unit2 to "meters".

8. Instantiate a Length object named length3 using the input from above, and print length3 using the toString method. Be sure to use a descriptive label.

9. Determine whether length2 is about equal to length3, and assign the result to a boolean variable. Print different messages depending on the value of the variable.

10. Assign the result of adding length2 and length3 to length4, and print length4 using the toString method. Be sure to use a descriptive label.

Reference no: EM13943429

Questions Cloud

Differences between quantitative and qualitative research : To help you organize your notes for this assignment, consider the following chart that provides an overview of differences between quantitative and qualitative research methodologies
How andres vs ymca case has impact sport facility managers : how this case has impacted sport facility managers today. Schneider v. American Hockey and Ice Skating Center, Inc. Body Max Fitness Center v. Sheffield. Olympia v. Johnson and Andres v. YMCA.
Heat transfer problem in a spherical cylinder : Non -zero heat flux component , since temperature changing in r - direction only , qr is present , the control volume , dr , as shown - Heat flux entering the control volume at r= r = qr = 4.π.r^2. qr|r
Find the equivalent monthly cost over the life of the car : Find the equivalent monthly cost over the life of the car of each of the three alternatives and select the lowest cost. (Disregard resale value at EOM 60, since it is the same in all three cases)
Write a program to construct and manipulate length objects : write a program to construct and manipulate Length objects that can handle any of the following units: meters, inches, feet, yards, miles.
Explain use of descriptive research within article study : The Effectiveness of Sign Prompts to Increase Hand Washing Behaviors in Restrooms Based on this article, answer the following: Explain the use of descriptive research within the article's study. Which aspects of this study would fall under the catego..
Difference between the two website forms : What was the main difference between the two website forms? Would the "NC Veterans Memorial" website benefit from the use of a Contact Us form? If so, why, and what fields should be included in the form?
Conceptual frameworks of the management : Understand theories and conceptual frameworks of the management of individual and organisational change
Private and public cloud storage and security concerns : You need to do the literature review on private and public cloud storage and Security concerns - Review the given document and minimize the similarity.

Reviews

Write a Review

Operating System Questions & Answers

  Crescent manufacturing inc cmi is a luxury leader in

crescent manufacturing inc. cmi is a luxury leader in crafted and customized home furnishings. the corporate

  Virtual machine design

In a virtual machine design where guest operating systems are independent virtual machines supported by a host operating system, what factors are involved in choosing the host operating system?

  Consider a web server with one cpu that serves web requests

Consider a web server with one CPU that serves web requests; each web request requires 3 (time) units of CPU processing and 9 (time) units to read data from disk.

  Identify the levels in the file hierarchy

Identify the levels in the file hierarchy and identify the order in which the directories need to be created and identify the commands used for creating the file hierarchy.

  Explain "information architectures" concept in detail

At the least, you need to iterate the questions a couple of times for yourself. Write down your answers, then set them aside for a couple of days and come back to them. If you can do this a couple of times, you'll find that your formulation will b..

  What instructions would you give people in a group session

What instructions would you give people in a group session about how to get their computers to look standardized?2. What policy would you suggest for what to put on the screen so it does not offend anyone?

  Describe three solutions to critical section problem

Describe three solutions to critical section problem Explain the different methods used to handle deadlocks Distinguish between "No preemption" and "circular wait" in deadlocks prevention

  Can we see new cultural values being shaped

Is the WWW changing users collectively or individually in profound or superficial ways?

  A computer has a cache, main memory, and a disk

A computer has a cache, main memory, and a disk. If a referenced word is in the cache, 20 ns are required to access it. If it is in main memory but not in the cache (called cache miss)

  Question about throughput and capacity

Throughput and capacity are determining factors for effectiveness of a system more so than processor speed?

  Operating systems assign a process number

Operating systems assign a process number to every running application program. Can you explain why these process numbers cannot be used instead of port numbers

  How does interrupt disabling provide mutual exclusion

In Peterson's Algorithm, if both processes want to enter the critical section, can one process repeatedly use the critical section if both have reached the while loops? Explain.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd