Question

For this assignment you will be creating an array of objects or a list of objects...

For this assignment you will be creating an array of objects or a list of objects of car parts based on user input.

1. Create a Parts class with the following items

a. Properties for PartNum, Part Name, Part Description, and Cost

b. Must have a constructor

2. In the main you will need accomplish the following:

a. Ask the user how many objects they wish to enter

b. Create an array of parts objects

c. Loop appropriately to collect the data needed to populate the objects in the array

d. Next you will do the following:

i. Give the user a menu of all parts

ii. Ask the user which part they would like to view data on

iii. Create a method to print out the object selected by user values.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

################# Parts.java ####################

/**
* The Class Parts.
*/
public class Parts {

   /** The part num. */
   private int partNum;

   /** The part name. */
   private String partName;

   /** The part description. */
   private String partDescription;

   /** The cost. */
   private double cost;

   /**
   * Instantiates a new parts.
   *
   * @param partNum the part num
   * @param partName the part name
   * @param partDescription the part description
   * @param cost the cost
   */
   public Parts(int partNum, String partName, String partDescription, double cost) {
       this.partNum = partNum;
       this.partName = partName;
       this.partDescription = partDescription;
       this.cost = cost;
   }

   /**
   * Gets the part num.
   *
   * @return the part num
   */
   public int getPartNum() {
       return partNum;
   }

   /**
   * Gets the part name.
   *
   * @return the part name
   */
   public String getPartName() {
       return partName;
   }

   /**
   * Gets the part description.
   *
   * @return the part description
   */
   public String getPartDescription() {
       return partDescription;
   }

   /**
   * Gets the cost.
   *
   * @return the cost
   */
   public double getCost() {
       return cost;
   }

   /**
   * To string.
   *
   * @return the string
   */
   @Override
   public String toString() {
       return "Parts [partNum=" + partNum + ", " + (partName != null ? "partName=" + partName + ", " : "")
               + (partDescription != null ? "partDescription=" + partDescription + ", " : "") + "cost=" + cost + "]";
   }

}

############# CarPartShop.java ###############

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CarPartShop {

   /**
   * The main method.
   *
   * @param args the arguments
   */
   public static void main(String[] args) {
       try {
           // Because of limitation of scanner. I'm using Buffer reader.
           BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
           System.out.println("Enter how many car parts you need to add: ");
           int numOfParts = Integer.parseInt(sc.readLine());
           Parts[] parts = new Parts[numOfParts];
           for (int i = 0; i < numOfParts; i++) {
               System.out.print("Enter part number: ");
               int partNum = Integer.parseInt(sc.readLine());
               System.out.print("Enter part name: ");
               String partName = sc.readLine();
               System.out.println("Enter description:    ");
               String partDesc = sc.readLine();
               System.out.println("Enter cost: ");
               double cost = Double.parseDouble(sc.readLine());
               parts[i] = new Parts(partNum, partName, partDesc, cost);
           }

           // showing menu.
           int partNum = showMenu(parts, sc);
           //showing part details
           for (Parts p : parts) {
               if (p.getPartNum() == partNum) {
                   System.out.println("Part Number: " + p.getPartNum());
                   System.out.println("Part Name: " + p.getPartName());
                   System.out.println("Part Description: " + p.getPartDescription());
                   System.out.println("Cost: $" + p.getCost());
               }
           }
       } catch (Exception e) {
           // TODO: handle exception
       }

   }

   /**
   * Show menu.
   *
   * @param parts the parts
   * @param sc the sc
   * @return the int
   * @throws NumberFormatException the number format exception
   * @throws IOException Signals that an I/O exception has occurred.
   */
   private static int showMenu(Parts[] parts, BufferedReader sc) throws NumberFormatException, IOException {
       for (Parts p : parts) {
           System.out.println("Part Number: " + p.getPartNum() + " Name: " + p.getPartName());
       }
       System.out.println("Enter part number to show details: ");
       return Integer.parseInt(sc.readLine());
   }
}

Add a comment
Know the answer?
Add Answer to:
For this assignment you will be creating an array of objects or a list of objects...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate...

    JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate passing array reference to a method;        III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...

  • C# Visual Studios HelloWorld For this assignment, you'll work with first creating an array and populating...

    C# Visual Studios HelloWorld For this assignment, you'll work with first creating an array and populating it's values. Then, we'll use the values in the array to calculate the average. Since it's common for an average to result in numbers with decimal points, the array you create should be of type double[]. This program will need to use dynamic input from the user so perform the following steps: Ask the user how many numbers need to be added. Use this...

  • In C# you will be creating a car dealership app that will contain an Automobile class...

    In C# you will be creating a car dealership app that will contain an Automobile class and three subclasses – Car, Truck, SUV. The properties for Automobile are as follows Base cost Model Make Year The Car will inherit Automobile and include the following: Tax break for better fuel option this calculation will be 2% The Truck will inherit Automobile and include the following: Incentive of $2000 off the cost The SUV will inherit Automobile and include the following: Extra...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • The purpose of this assignment is to get experience with an array, do while loop and...

    The purpose of this assignment is to get experience with an array, do while loop and read and write file operations. Your goal is to create a program that reads the exam.txt file with 10 scores. After that, the user can select from a 4 choice menu that handles the user’s choices as described in the details below. The program should display the menu until the user selects the menu option quit. The project requirements: It is an important part...

  • Use your Car class from the previous Programming Assignment. For this assignment, create a new class...

    Use your Car class from the previous Programming Assignment. For this assignment, create a new class that represents a Used Car Dealership. Your Java program will simulate an inventory system for the dealership, where the employees can manage the car information. Start by creating an array of 10 cars, with an "ID" of 0 through 9, and use the default constructor to create each car. For example, the third car in an array called cars would have ID 2. You...

  • The goal of this assignment is to give you some experience building a program that uses...

    The goal of this assignment is to give you some experience building a program that uses objects. You must work in teams of 2 to complete this assignment. You will use the BankAccount class you created in Lab 3 to create a banking program. The program must display a menu to the user to allow the user to select a transaction like deposit, withdraw, transfer, create a bank account, and quit the program. The program should allow a user to...

  • Java - Object Oriented Programming From the game slither.io Identify objects that you can see ...

    Java - Object Oriented Programming From the game slither.io Identify objects that you can see on the picture. Pick one object from part a. Create a class (Java code) for that object which contain data member, method and constructor. Implement the class inside main function by creating that object.

  • In this assignment you will create two Java programs: The first program will be a class...

    In this assignment you will create two Java programs: The first program will be a class that holds information (variables) about an object of your choice and provides the relevant behavior (methods) The second program will be the main program which will instantiate several objects using the class above, and will test all the functions (methods) of the class above. You cannot use any of the home assignments as your submitted assignment. Your programs must meet the following qualitative and...

  • Array Class Assignment

    This is a two-part assignment. It is better to submit the files for both parts when you are done than to submit part 1 first and part 2 later.In the Array.h file includes the class definition and member functions implementations.PART 1Write And Test An Array Class [Array.TestDriver.cpp] Write a data structures class. The resulting class can be used in any program in place of a C++ array, in case you want the advantages of range safety and built-in size tracking.Requirements....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT