Question

Java Programming Question

After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would like to sell any extra weight we might have. We agree to sell him some items, however, we do not want to sell just anything, as some of our loot might have a better credit rate elsewhere. Unfortunately, we are unable to tell what kind of item is what, so we are unable to make the sale. On our way to the nearest space station, we think of a way to classify the items in our cargo bay. We decide that it would be best to classify the items in our hold as equipmentconsumable, or material. When we arrive at the station, we create a clear hierarchy for all of our items. After fencing our loot, we decide to rest at this station for the night.

  • Items have attributes such as Name, Weight, Value, Durability, and ID. (Create an object called 'Item').

  • We now classify our items by separating them into 3 distinct categories Equipment, Consumable, or Material. (You must implement these 3 classes that are subclasses of Item and they must have at least 3 unique attributes in each subclass)

  • We can carry up to 10 items, as long as they don't exceed the maximum weight of the cargo bay, 25 Tons. (Use an Array that checks an item's weight before placing it in the cargo hold)

  • We need to be able to add and remove items by their name.

  • We need to be able to search for a specific type of item in our cargo bay based on the item's name and one of its attributes (Implement 2 searches - one on the name and another on any attribute you choose).

  • We need to be able to sort items by their names alphabetically in ascending order (Using an Insertion or Selection sort only)

  • We need to know how many of each item we have in our cargo bay and display their attributes.

The driver you are to use has been attached to the module.

Note: You CANNOT modify the AssignmentDriver file, you should only modify the file called StudentMethods.java and you CANNOT change the method headers. You can add other methods as you see fit, but do not modify any of the method headers I have given you. This will break the testing program I have, and will result in a lower grade on your assignment.


Use the following code as your base.


package Assignments;



import java.util.Scanner;


public class Assignment03Driver {

StudentMethods03 studentMethods = new StudentMethods03();

Scanner input = new Scanner(System.in);


public static void main(String[] args) {

new Assignment03Driver();

}


public Assignment03Driver() {

Item[] cargohold = new Item[10];

System.out.println("Welcome to the Discipulus Cargo Hold interface.");

System.out.println("Please select a number from the options below");

System.out.println("");

while (true) {

// Give the user a list of their options

System.out.println("1: Add an item to the cargo hold.");

System.out.println("2: Remove an item from the cargo hold.");

System.out.println("3: Sort the contents of the cargo hold.");

System.out.println("4: Search for an item by name.");

System.out.println("5: Search for an item by ID.");

System.out.println("6: Display the items in the cargo hold.");

System.out.println("0: Exit the Discipulus Cargo Hold interface.");

int userChoice = input.nextInt();

input.nextLine();

switch (userChoice) {

case 1:

studentMethods.addItem(cargohold);

break;

case 2:

studentMethods.removeItem(cargohold);

break;

case 3:

studentMethods.sortItems(cargohold);

break;

case 4:

studentMethods.searchItemsByName(cargohold);

break;

case 5:

studentMethods.searchItemsByID(cargohold);

break;

case 6:

studentMethods.displayItems(cargohold);

break;

case 0:

System.out.println("Thank you for using the Discipulus Cargo Hold interface. See you again soon!");

System.exit(0);

}

}

}

}



package Assignments;



import java.util.Scanner;


public class StudentMethods03 {

  Item[] cargohold;

  int numItems;

  Scanner input = new Scanner(System.in);

   

  public static void main(String[] args) {

     new StudentMethods03();

      

  }


  public StudentMethods03() {

     cargohold = new Item[10];

     numItems = 0;

      

     System.out.println("Welcome to the BlackStar Cargo Hold interface.");

     System.out.println("Please select a number from the options below");

     System.out.println("");

     while (true) {

       

        System.out.println("1: Add an item to the cargo hold.");

        System.out.println("2: Remove an item from the cargo hold.");

        System.out.println("3: Sort the contents of the cargo hold.");

        System.out.println("4: Search for an item.");

        System.out.println("5: Display the items in the cargo hold.");

        System.out.println("0: Exit the BlackStar Cargo Hold interface.");

         

        int userChoice = input.nextInt();

        input.nextLine();

         

        switch (userChoice) {

        case 1:

           addItem();

           break;

        case 2:

           removeItem();

           break;

        case 3:

           sortItems();

           break;

        case 4:

           searchItems();

           break;

        case 5:

           displayItems();

           break;

        case 0:

           System.out.println("Thank you for using the BlackStar Cargo Hold interface. See you again soon!");

           System.exit(0);

            

        }

         

     }

      

  }

   

  private void addItem() {

     if (numItems == cargohold.length) {

        System.out.println("Cargo Hold is full, remove some items first");

        return;

     }  

      

     System.out.println("Enter Item Name: ");

     String name = input.nextLine();

      

     System.out.println("Enter Weight: ");

     double weight = Double.parseDouble(input.nextLine());

      

     System.out.println("Enter Value: ");

     double value = Double.parseDouble(input.nextLine());

      

     System.out.println("Enter durability: ");

     int durability = Integer.parseInt(input.nextLine());


     System.out.println("Enter Numeric ID: ");

     int id = Integer.parseInt(input.nextLine());


     Item item = new Item(name, weight, value, durability, id);

     cargohold[numItems] = item;

     numItems++;


     System.out.println("Item added successfully!");

      

  }

   

  private void removeItem() {

     if (numItems == 0) {

        System.out.println("Cargo hold is empty!");

        return;

     }


     System.out.println("Enter item name");

     String name = input.nextLine();

     int index = searchByName(name);

     if (index == -1) {

       

        System.out.println("Item doesn't exist in the Cargo Hold");

     } else {

        for (int i = index; i < numItems - 1; i++) {

           cargohold[i] = cargohold[i + 1];

        }

        numItems--;

         

        System.out.println("Item removed successfuly");


     }


  }


  private void sortItems() {

  for (int i = 0; i < numItems - 1; ++i) {

        int min = i;

        for (int j = i + 1; j < numItems; ++j) {

           if (cargohold[j].getName().compareTo(cargohold[min].getName()) < 0) {

              min = j;

           }

        }

        Item temp = cargohold[i];

        cargohold[i] = cargohold[min];

        cargohold[min] = temp;

     }

     System.out.println("Items are sorted!");


  }

   

  private void searchItems() {

     System.out.println("1. Search by Name");

     System.out.println("2. Search by ID");

     int choice = Integer.parseInt(input.nextLine());

     if (choice == 1) {

       

        System.out.println("Enter name: ");

        String name = input.nextLine();

        int index = searchByName(name);

        if (index == -1) {

        

           System.out.println("Not found");

        } else {

        

           System.out.println("Item found at position " + index);

           System.out.println("Name: " + cargohold[index].getName());

           System.out.println("ID: " + cargohold[index].getID());

        }

     } else if (choice == 2) {

       

        System.out.println("Enter ID: ");

        int id = Integer.parseInt(input.nextLine());

        int index = searchByID(id);

        if (index == -1) {

        

           System.out.println("Not found");

        } else {

        

           System.out.println("Item found at position " + index);

           System.out.println("Name: " + cargohold[index].getName());

           System.out.println("ID: " + cargohold[index].getID());

        }

     }

  }


  private int searchByName(String name) {

     for (int i = 0; i < numItems; i++) {

        if (cargohold[i].getName().equalsIgnoreCase(name)) {

           return i;

        }

     }

     return -1;

  }

   

  private int searchByID(int ID) {

     for (int i = 0; i < numItems; i++) {

        if (cargohold[i].getID() == ID) {

           return i;

        }

     }

     return -1;

  }


  private void displayItems() {

     Item[] uniqueItems = new Item[numItems];     

     int[] counts = new int[numItems];

     int counter = 0;

     for (int i = 0; i < numItems; i++) {

        int foundIndex = -1;

        String name = cargohold[i].getName();

        for (int j = 0; j < counter; j++) {

           if (uniqueItems[j].getName().equalsIgnoreCase(name)) {

              foundIndex = j;

           }

        }

        if (foundIndex == -1) {

           uniqueItems[counter] = cargohold[i];

           counts[counter]++;

           counter++;

        } else {          

           counts[foundIndex]++;

        }

     }

     for (int i = 0; i < counter; i++) {

        System.out.println("Item Name: " + uniqueItems[i].getName()

              + ", count - " + counts[i]);

     }


  }


public void addItem(Item cargohold[]) {

}


public void removeItem(Item cargohold[]) {

}


public void sortItems(Item cargohold[]) {


}


public void searchItemsByName(Item cargohold[]) {

}


public void searchItemsByID(Item cargohold[]) {

}


public void displayItems(Item cargohold[]) {

// TODO: Display only the unique items along with a count of any duplicates

// For example it should say

// Food - 2

// Water - 3

// Ammunition - 5

}






}


package Assignments;


public class Item {


   private String name;

   private double weight;

   private double value;

   private int durability;

   private int ID;



   public Item(){

    

   }


   public Item(String name, double weight, double value, int durability, int iD) {

      this.name = name;

      this.weight = weight;

      this.value = value;

      this.durability = durability;

      ID = iD;

   }


   /* getters and setters

    * */


   public String getName() {

      return name;

   }

   public void setName(String name) {

      this.name = name;

   }

   public double getWeight() {

      return weight;

   }

   public void setWeight(double weight) {

      this.weight = weight;

   }

   public double getValue() {

      return value;

   }

   public void setValue(double value) {

      this.value = value;

   }

   public int getDurability() {

      return durability;

   }

   public void setDurability(int durability) {

      this.durability = durability;

   }


   public int getID() {

      return ID;

   }

   public void setID(int iD) {

      ID = iD;

   }


}


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

package Project;


import java.util.Arrays;

import java.util.Collections;

import java.util.Scanner;


import Item.ComparableItem;


import java.util.ArrayList;


public class Assignment3 {

   Scanner input = new Scanner(System.in);

   

   public static void main(String[] args) {

      new Assignment3();

   }

   

   // This will act as our program switchboard

   public Assignment3() {

      

      ArrayList<Item> cargohold = new ArrayList<Item>();

      

      System.out.println("Welcome to the BlackStar Cargo Hold interface.");

      System.out.println("Please select a number from the options below");

      System.out.println("");

      

      while (true) {

         // Give the user a list of their options

         System.out.println("1: Add an item to the cargo hold.");

         System.out.println("2: Remove an item from the cargo hold.");

         System.out.println("3: Sort the contents of the cargo hold.");

         System.out.println("4: Search for an item.");

         System.out.println("5: Display the items in the cargo hold.");

         System.out.println("0: Exit the BlackStar Cargo Hold interface.");

         

         // Get the user input

         int userChoice = input.nextInt();

         input.nextLine();

         

         switch (userChoice) {

            case 1:

            addItem(cargohold);

            break;

            case 2:

            removeItem(cargohold);

            break;

            case 3:

            sortItems(cargohold);

            break;

            case 4:

            searchItems(cargohold);

            break;

            case 5:

            displayItems(cargohold);

            break;

            case 0:

            System.out.println("Thank you for using the BlackStar Cargo Hold interface. See you again soon!");

            System.exit(0);

            default:

            System.out.println("Invalid value. Choose a number 0-5 only.");

            break;

         }

      }

      

   }

   

   private void addItem(ArrayList<Item> cargohold) {

      cargohold.add(new Item());

      int index = cargohold.size();

      Item tempItem = new Item();

      System.out.println("Enter the Item's name");

      String userInput = input.nextLine();

      tempItem.setItemName(userInput);

      System.out.println("Enter the Item's weight");

      userInput = input.nextLine();

      tempItem.setItemWeight(userInput);

      System.out.println("Enter the Item's value");

      userInput = input.nextLine();

      tempItem.setItemValue(userInput);

      System.out.println("Enter the Item's durability");

      userInput = input.nextLine();

      tempItem.setItemDurability(userInput);

      System.out.println("Enter the Item's ID");

      userInput = input.nextLine();

      tempItem.setItemID(userInput);

      

      cargohold.set(index - 1, tempItem);

      System.out.println("Item added successfully");

      return;

   }

   

   private void removeItem(ArrayList<Item> cargohold) {

      int index = cargohold.size();

      

      if(index == 0) {

         System.out.println("The cargohold has no items to remove!");

         return;

      } else {

         

         System.out.println("Enter the name of the item to be removed.");

         String userInput = input.nextLine();

         

         for(int i = 0; i < cargohold.size(); i++) {

            cargohold.get(i);

            if(Item.name.equals(userInput)) {

               cargohold.get(i);

               Item.name = "none";

               cargohold.get(i);

               Item.weight = "none";

               cargohold.get(i);

               Item.value = "none";

               cargohold.get(i);

               Item.durability = "none";

               cargohold.get(i);

               Item.ID = "none";

               System.out.println("Item removed.");

               break;

            } else if (i == cargohold.size() - 1) {

               System.out.println("That item is not in the cargohold.");

            }

         }

         return;

      }

   }

   

   private void sortItems(ArrayList<Item> cargohold) {

      int index = cargohold.size();

      

      if(index <= 1) {

         System.out.println("The cargohold has too few items to sort.");

         return;

      } else {

         System.out.println(compareTo(cargohold.get(1), cargohold.get(2)));

      }

      /*

      for(int x = 0; x < 5; x++) { //this for loop repeats the sorting process according to the length of the array to enhance sorting accuracy

      for (int i = 0; i < cargohold.size(); i++) {

      int minimum = i;

      //System.out.println(minimum);

      for (int j = i + 1; j < cargohold.length; j++) {

      //System.out.println(j);

      //System.out.println(cargohold[i].compareTo(cargohold[minimum]));

      if(!cargohold[j].name.equals("none")) {

      if(cargohold[j].name.compareTo(cargohold[minimum].name) < 0) {

      minimum = j;

      //Debug Note: Outputs array reassignments in real time.

      //System.out.println("Swapping " + cargohold[i] + " with " + cargohold[minimum]);

      //System.out.println(Arrays.asList(cargohold));

      Item transfer = cargohold[i];

      cargohold[i] = cargohold[minimum];

      cargohold[j] = transfer;

      }

      }

      }

      }

      }

      System.out.println("cargohold sorted.");*/

      

      return;

   }

   

   private void searchItems(ArrayList<Item> cargohold) {

      System.out.println("Enter the name of the item you wish to search for.");

      String userInput = input.nextLine();

      

      for(int i = 0; i < cargohold.size(); i++) {

         if (userInput.equals(cargohold.get(i).name)) {

            System.out.println("The item was found at section number " + (i + 1));

            return;

         }

      }

      System.out.println("The item was not found in the cargo hold.");

   }

   

   private void displayItems(ArrayList<Item> cargohold) {

      while (true) {

         // Give the user a list of their options

         System.out.println("1: Display all items in the cargohold");

         System.out.println("2: Display a specific item in the cargohold");

         

         // Get the user input

         int userChoice = input.nextInt();

         input.nextLine();

         

         switch (userChoice) {

            case 1:

            //simple output

            for(int i = 0; i < cargohold.size(); i++) {

               cargohold.get(i);

               System.out.println(Item.name);

               cargohold.get(i);

               System.out.println(Item.weight);

               cargohold.get(i);

               System.out.println(Item.value);

               cargohold.get(i);

               System.out.println(Item.durability);

               cargohold.get(i);

               System.out.println(Item.ID);

               

            }

            return;

            case 2:

            

            return;

            default:

            System.out.println("Invalid value. Choose a number 1-2 only.");

            break;

         }

      }

   }

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Java Programming Question
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

  • CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE....

    CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE. SAME CODE SHOULD BE USED FOR THE DOCUMENTATION PURPOSES. ONLY INSERT THE JAVA DOC. //Vehicle.java public class Vehicle {    private String manufacturer;    private int seat;    private String drivetrain;    private float enginesize;    private float weight; //create getters for the attributes    public String getmanufacturer() {        return manufacturer;    }    public String getdrivetrain() {        return drivetrain;...

  • Hello, I'm very new to programming and I'm running into a problem with the system.out.println function...

    Hello, I'm very new to programming and I'm running into a problem with the system.out.println function in the TestAccount Class pasted below. Account Class: import java.util.Date; public class Account {    private int accountId;    private double accountBalance, annualInterestRate;    private Date dateCreated;    public Account(int id, double balance) { id = accountId;        balance = accountBalance; dateCreated = new Date();    }    public Account() { dateCreated = new Date();    }    public int getId() { return...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...

    Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then generate their documentation. Examine the documentation to see the logic used in creating each class. (Code given below) import java.util.ArrayList; import java.util.Iterator; class Calendar { private Year year; public Calendar() { year = new Year(); } public void printCalendar() { year.printCalendar(); } } class Year { private ArrayList<Month> months; private int number; public Year() { this(2013); } public Year(int number) { this.number = number;...

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
ADVERTISEMENT