Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSCI 1302- Advanced Programming Principles Armstrong State University Computer S

ID: 3882624 • Letter: C

Question



CSCI 1302- Advanced Programming Principles Armstrong State University Computer Science and Information Technology Fall 2017 Point Value: 15 points Due: Thursday September 7, 2017, 4:30 pm Description 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 Accessor and mutator methods for all data members. A method named getinfo) 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 values. .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 members.

Explanation / Answer

import java.io.*;

public class PAssign3 {
    public static void main(String[] args){

        Engine e1 = new Engine();
        Engine e2 = new Engine(8);

        Vehicle v1 = new Vehicle();
        v1.setEngine(e1);
        Vehicle v2 = new Vehicle("Hyunzda","Sonius", e2);

        System.out.println(v1.getInfo());
        System.out.println(v2.getInfo());
    }
}

class Vehicle{
   private String make;
   private String model;
   private Engine eng;
   public Vehicle(){
       make = "Toyabu";
       model = "Priata";
   }
   public Vehicle(String a, String b, Engine e){
        make = a;
        model = b;
        eng = e;
   }
   public String getMake(){
        return make;
   }
   public String getModel(){
        return model;
   }
   public Engine getEngine(){
        return eng;
   }
   public void setMake(String a){
      make = a;
   }
   public void setModel(String a){
      model = a;
   }
   public void setEngine(Engine a){
      eng = a;
   }
   public String getInfo(){
       return make + " " + model + " " + String.valueOf(eng.getNumCyllinder());
   }
}

class Engine{

    private int num_cyllinder;
    public Engine(){
       num_cyllinder = 4;
    }
    public Engine( int a){
       num_cyllinder = a;
    }
    public int getNumCyllinder(){
       return num_cyllinder;
    }
    public void setNumCyllinder( int a){
       if (a < 4)
          num_cyllinder = 4;
       else
          num_cyllinder = a;
    }

}

The relation between Vehicle class and Engine class is one to one.

     Vehicle
make:String
model:String
eng:Engine

getMake():String
getModel():String
getEngine():Engine
setMake(a:String):void
setModel(a:String):void
setEngine(a:Engine):void
getinfo():String


      Engine
num_cyllinder:int

getNumCyllinder():int
setNumCyllinder(a:int):void