Program10.java Part 1: requires three files:  Design and save your Motor class before you make Vehicle, so you can specify Motor as a Vehicle data attribute. Class Motor (make this class first because Motor is an attribute of class Vehicle) Attributes (all private) int cylinders int hp String type (possible values being gas, deisel, electric etc) Methods (all public) a constructor that can assign values to all attributes getters and for each attribute (setters not needed) a toString method that returns the status of a Motor instance, all attributes. Class Vehicle Attributes (all private) String make String model int year double price Motor motor (see below) Methods (public) a constructor that can assign values to all attributes a setter for the price a getter for the price a toString method that returns the status of a Vehicle instance, all attributes. Class TestVehicle This is the executable class. In the main method, make an arraylist of five or six Vehicle instances and then use a foreach loop to display them.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter8: Arrays
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

Program10.java

Part 1: requires three files: 
Design and save your Motor class before you make Vehicle, so you can specify Motor as a Vehicle data attribute.

Class Motor (make this class first because Motor is an attribute of class Vehicle)
Attributes (all private)

  • int cylinders
  • int hp
  • String type (possible values being gasdeiselelectric etc)

Methods (all public)

  • constructor that can assign values to all attributes
  • getters and for each attribute (setters not needed)
  • toString method that returns the status of a Motor instance, all attributes.

Class Vehicle
Attributes (all private)

  • String make
  • String model
  • int year
  • double price
  • Motor motor (see below)

Methods (public)

  • a constructor that can assign values to all attributes
  • a setter for the price
  • a getter for the price
  • a toString method that returns the status of a Vehicle instance, all attributes.

Class TestVehicle
This is the executable class. In the main method, make an arraylist of five or six Vehicle instances and then use a foreach loop to display them.

Part 2

1. Write a Java program that prompts the user to enter a security code that matches a specific pattern. Your program must approve the user's entry. The pattern consists of these characters in this sequence:

A lower case character, two upper case characters, two or three digits, two lower case characters, an upper case character, 2 or 3 lower case characters, 2 digits.

Refer to Appendix H beginning on pages 1176
.
Example Run

Enter the code pattern
bHT482haQmo82
Yes, bHT482haQmo82 matches the pattern

Recheck the requirements before you submit!

Expert Solution
Program 10

Kindly Note: As per our guidelines we are supposed to answer only first question. Kindly repost other questions as a separate question.

 

import java.util.*;  
class Motor
{
    private int cylinders, hp;
    private String type;
    
    //Constructor
    public Motor(int c, int h, String t)
    {
        cylinders = c;
        hp = h;
        type = t;
    }
    
    //Getters
    public int getCylinders() { return cylinders; }
    public int getHp() { return hp; }
    public String getType() { return type; }
    
    public String toString()
    {
        String str = "Type: " + type + "\nCylinders: " +  cylinders + "\nHP: " + hp;
        return str;
    }
}

class Vehicle
{
    private String make, model;
    private int year;
    private double price;
    private Motor motor;
    
    //Constructor
    public Vehicle(String ma, String mo, int y, double pr, Motor m)
    {
        make = ma;
        model = mo;
        year = y;
        price = pr;
        motor = m;
    }
    
    //a setter for the price
    public void setPrice(double pr) { price = pr; }
    
    //a getter for the price
    public double getPrice() { return price; }
    
    public String toString()
    {
        String str = motor.toString();
        str += "\nMake: " + make + "\nModel: " + model + "\nYear: " + year + "\nPrice: " + price + "\nMotor: " + motor +"\n";
        return str;
    }
}
public class Main //TestVehicle
{
 public static void main(String[] args) 
 {
     //Creating 5 instances of class Motor
     Motor m1 = new Motor(1, 100, "ABC");
     Motor m2 = new Motor(2, 200, "XYZ");
     Motor m3 = new Motor(3, 300, "LMN");
     Motor m4 = new Motor(4, 400, "PQR");
     Motor m5 = new Motor(5, 500, "STU");
     
     //arraylist of five Vehicle instances 
     Vehicle v1 = new Vehicle("hj", "jk", 2000, 10000, m1);
     Vehicle v2 = new Vehicle("ty", "op", 2001, 20000, m2);
     Vehicle v3 = new Vehicle("hj", "kl", 2002, 30000, m3);
     Vehicle v4 = new Vehicle("fr", "hj", 2003, 40000, m4);
     Vehicle v5 = new Vehicle("wh", "fg", 2004, 50000, m5);
     
     ArrayList<Vehicle> list=new ArrayList<Vehicle>();
     list.add(v1);
     list.add(v2);
     list.add(v3);
     list.add(v4);
     list.add(v5);
     
  for(Vehicle x:list)    
            System.out.println(x);    
 }
}

 

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT