Typical Examination questions at the beginning and your answers at the end. Ther
ID: 3753054 • Letter: T
Question
Typical Examination questions at the beginning and your answers at the end. There are 75 points available Please answer the questions in a blank sheet of US letter paper, provided at the front. This makest easier to scan for electronic storage and delivery. Both blank and lined paper are provided. Java You're going to write a class called Distance that embodies the idea of a distance. It will store the distance in terms of meters. Write the statement that is used to start the declaration of the class. (5 points) 2. 1. Write the all-arguments constructor for the class Distance. (10 points) 3. Write the no arguments ("default") constructor for the class Distance. (10 points) 4. Write an accessor (getter) method for the distance represented in meters. (10 points) 5. Write a mutator (setter) method for the distance. (10 points) 6. Write a virtual accessor method that allows access to the distance in feet. You can assume there are 3.28 feet in each meter. (10 points)Explanation / Answer
Here is the completed code for Distance.java class. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Distance.java
public class Distance { // defining a public class named Distance
// private attribute to store distance in meters
private double distanceInMeters;
/**
* constructor with an argument to initialize distanceInMeters
*
* @param distanceInMeters
* distance in meters
*/
public Distance(double distanceInMeters) {
this.distanceInMeters = distanceInMeters;
}
/**
* default constructor, initializing distanceInMeters to 0
*/
public Distance() {
distanceInMeters = 0;
}
/**
* getter method for fetching distance in meters
*
* @return distanceInMeters
*/
public double getDistanceInMeters() {
return distanceInMeters;
}
/**
* setter method for mutating distance in meters
*
* @param distanceInMeters
* - new distance in meters
*/
public void setDistanceInMeters(double distanceInMeters) {
this.distanceInMeters = distanceInMeters;
}
/**
* virtual accessor method for fetching distance in feet
*
* @return distanceInMeters converted to feet
*/
public double getDistanceInFeet() {
// 1 meter= 3.28 feet, therefore multiplying with 3.28 to find total
// distance in feet. You may define 3.28 as a constant if you like
return distanceInMeters * 3.28;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.