AT&T; 10:10 PM armstrong.view.usg.edu CSCI 1302-Advanced Programming Principles
ID: 3890002 • Letter: A
Question
AT&T; 10:10 PM armstrong.view.usg.edu CSCI 1302-Advanced Programming Principles Armstrong State University Computer Science and Information Technology Fall 2017 Assignment 3 Point Value: 15 points Due: Thursday September 7, 2017, 4:30 pm Write a Java program to define and test a Vehicle class that aggregate an Engine class. The Vehicle class represents a single vehicle. For the purpose of this assignment, assume that each of these vehicles contains an Engine. The engine is manufactured independently and can be used in other vehicles Author/Designer Role The Vehicle class should contain: . An Engine data member named engine that contains an Engine instance. This engine defaults to 4 cylinders, but can be 6 or 8 cylinders. * A String make that sets the make for the vehicle. This should default to "Toyabu" . A String model that sets the model for the vehicle. This should default to "Priata .A default no-arg constructor that creates a default Vehicle. Set your default values here by using the appropriate set methods. . A constructor that allows the client to set the engine, vehicle make, and vehicle model e Accessor and mutator methods for all data members. . A method named getinfol) that returns a String indicating the make, model, and engine's number of cylinders. See sample output for the needed format. The Engine class should contain: . An int data member named cylinders that indicates the number of cylinders. This should default to 4. In the mutator, make sure the value being set is not less than 4. If so, set it to 4. A default no-arg constructor that creates a default Engine. Set your default values here by using calling the convenience constructor with the this keyword and the appropriate default . A convenience constructor that allows the client to set the number cylinders. Accessor and mutator methods for all data members. Mutators should check values as described above NOTES: Use appropriate visibility modifiers based on class discussion. Use accessor/mutator methods and the this keyword when accessing data membersExplanation / Answer
Engine.java
public class Engine {
// Declaring instance variable
private int noOfCylinders;
// Zero argumented constructor
public Engine() {
this(4);
}
// Parameterized constructor
public Engine(int noOfCylinders) {
this.noOfCylinders = noOfCylinders;
}
// getters and setters
public int getNoOfCylinders() {
return noOfCylinders;
}
public void setNoOfCylinders(int noOfCylinders) {
if (noOfCylinders < 4)
this.noOfCylinders = 4;
else
this.noOfCylinders = noOfCylinders;
}
}
________________
Vehicle.java
public class Vehicle {
// Creating An Engine class instance by passing nonof cylinders as arguments
Engine engine = new Engine(4);
// Declaring instance variables
String make;
String model;
// Zero argumented constructor
public Vehicle() {
setMake("Toyabu");
setModel("Priata");
}
// Parameterized constructor
public Vehicle(Engine engine, String make, String model) {
super();
this.engine = engine;
this.make = make;
this.model = model;
}
// getters and setters
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
// This method will display the info about make model and no of cylinders
public String getInfo() {
return make + " " + model + " Engine: " + engine.getNoOfCylinders() + " cylinders";
}
}
____________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating Engine class instance variables
Engine e1, e2;
//Assigning the Engine class objects to references
e1 = new Engine(4);
e2 = new Engine(8);
//Creating Vehicle class reference variables
Vehicle v1, v2;
//Assigning the Vehicle class objects to references
v1 = new Vehicle(e1, "Toyabu", "Priate");
v2 = new Vehicle(e2, "Hyunzda", "Sonius");
//Displaying the vehicle class object info
System.out.println(v1.getInfo());
System.out.println(v2.getInfo());
}
}
__________________
Output:
Toyabu Priate
Engine: 4 cylinders
Hyunzda Sonius
Engine: 8 cylinders
_____________Could you rate me well.Plz .Thank You