***PLEASE HELP! FULL POINTS WILL BE GIVEN IF COMPLETED PROPERLY*** Programming l
ID: 3846783 • Letter: #
Question
***PLEASE HELP! FULL POINTS WILL BE GIVEN IF COMPLETED PROPERLY***
Programming language: JAVA
For this HW, you are to create the following:
A class object called "Car." (Full points ONLY if invariants are checked properly everywhere. "Checked properly" includes throwing an exception when necessary)
-(2 Points) A constructor
-(2 Points) An overloaded constructor
-(2 Points) At least two properties (and the corresponding getters/setters)
-(2 Points) having at least one array type property
-(2 Points) having at least one non-array reference type property
-(2 Points) A method that takes in a parameter and returns a value (other than getters/setters)
-(2 Points) A method that takes in at least two parameters and doesn't return a value (modifies the class properties)
-(2 Points) A method that loops through an array to manipulate or gather data from the array
-(2 Points) a toString() method
-(2 Points) an equals() method
A driver for your class object
-(2 Points) for testing creating valid and invalid instances of the class object
-(4 Points) for testing every public method (including getters and setters) with valid and invalid values (i.e. test the invariants)
-(4 Points) for testing the boundary conditions (lower and upper boundaries) for every public methods in the class (including getters and setters)
Explanation / Answer
import java.util.Arrays;
public class Car {
String makeModel;
int year;
int speed;
int[] arr;
public Car() {
// TODO Auto-generated constructor stub
this.makeModel = "Toyota Camry";
this.year = 2016;
this.speed = 0;
arr = new int[5];
}
/**
* @param makeModel
* @param year
*/
public Car(String makeModel, int year) {
this.makeModel = makeModel;
this.year = year;
this.speed = 0;
arr = new int[5];
}
/**
* @param makeModel
* @param year
*/
public void setMakeModelYear(String makeModel, int year) {
this.makeModel = makeModel;
this.year = year;
this.speed = 0;
}
/**
* @return the makeModel
*/
public String getMakeModel() {
return makeModel;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @param makeModel
* the makeModel to set
*/
public void setMakeModel(String makeModel) {
this.makeModel = makeModel;
}
/**
* @param year
* the year to set
*/
public void setYear(int year) {
this.year = year;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((makeModel == null) ? 0 : makeModel.hashCode());
result = prime * result + year;
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Car))
return false;
Car other = (Car) obj;
if (makeModel == null) {
if (other.makeModel != null)
return false;
} else if (!makeModel.equals(other.makeModel))
return false;
if (year != other.year)
return false;
return true;
}
/**
* @return the arr
*/
public int[] getArr() {
return arr;
}
/**
* @param arr
* the arr to set
*/
public void setArr(int[] arr) {
this.arr = arr;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Car [makeModel=" + makeModel + ", year=" + year + ", speed="
+ speed + ", arr=" + Arrays.toString(arr) + "]";
}
}
public class CarTest {
public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car("Honda", 2011);
int[] arr = { 1, 2, 3, 4, 5 };
car2.setArr(arr);
System.out.println(car1);
System.out.println(car2);
}
}
OUTPUT:
Car [makeModel=Toyota Camry, year=2016, speed=0, arr=[0, 0, 0, 0, 0]]
Car [makeModel=Honda, year=2011, speed=0, arr=[1, 2, 3, 4, 5]]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.