Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 324913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:10:04+00:00 2026-05-12T09:10:04+00:00

This is homework, but I need a nudge. I can’t find how to sort

  • 0

This is homework, but I need a “nudge”. I can’t find how to sort by name.

My Questions: (Please keep answers beginner friendly)

  1. Does what have look right so far?
  2. How to sort?
  3. Does anyone have recommendations for optimizing/cleaning this?

Here is the assignment:

The Inventory Program Part 2 checkpoint that is due in week 6 has the following requirements:

  1. Modify the Inventory Program so the application can handle multiple items. Use an array to store the items.

  2. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product.

  3. In addition, the output should display the value of the entire inventory.

  4. Create a method to calculate the value of the entire inventory.

  5. Create another method to sort the array items by the name of the product.

To satisfy these requirements, you will need to add the following to your Inventory class (not product class):

1) Declare an array of type Product (a private instance variable)

2) Inside your main while loop, do the following:

a. Instantiate a product object 
b. Populate the product object with the input from the console (like Inventory Part 1     does) 
c. Add the product object reference to the next element in your array 
d. Destroy the reference to the product object variable (note this object was added to your array so you can set the local variable that refers to the object to null)

3) Outside of your main while loop, do the following:

a. Call a method in your Inventory class to sort the array of Product objects. Note you need to sort an array of Product objects by the Product Name instance variable. To do this, you will need to create a separate .java file that implements the Comparator interface. You will pass the Product array and implemented Comparator interface class as arguments to the sort method of the Arrays class (part of the Java API). 
b. Create a For Loop that iterates through the array of Product objects (similar to the one I have below). Invoke the get method on each instance variable and format the output by calling printf. Sample For Loop to use:
for ( Product product : productArray )  
    {           Add statements here  
    }
c. Call a method in your Inventory class to calculate the total inventory value of all the Product objects in your array. 

Here is my code

    public class InvTest2{// main method begins
       public static void main( String args[] ) {

        int version = 2;// Version number
        final int invLeng = 5;// Declare Inv length

       // Welcome message
       System.out.printf( "\n%s%d\n" , 
       "Welcome to the Inventory Program v.", version );

       Inv[] DVDs = new Inv[invLeng];
       DVDs[0] = new Inv("The Invisible Man", 0, 8.50); // new DVD constructor
       DVDs[1] = new Inv("The Matrix", 1, 17.99);
       DVDs[2] = new Inv("Se7en", 7, 12.99);
       DVDs[3] = new Inv("Oceans Eleven", 11, 9.99);
       DVDs[4] = new Inv("Hitch Hikers Guide to the Galaxy", 42, 18.69);

        // Display formatted results
        int c = 0;
        double runningValue = 0;
        System.out.printf( "\n%s\n", "Inventory of DVD movies");
        while(c != DVDs.length){
        System.out.printf( "\n\n%s%d\n%s%s\n%s%d\n%s%,.2f\n%s%,.2f\n",
        "Item Number:      ",c,      
        "DVD Title:        ",DVDs[c].getdvdTitle(),
        "Copies in stock:  ",DVDs[c].getdvdInStock(),
        "Price each disk:  $",DVDs[c].getdvdValue(),
        "Value of copies:  $",DVDs[c].getdvdStockValue());//End print
        runningValue += DVDs[c].getdvdStockValue();
        c++;
        }
        System.out.printf( "\n%s%,.2f\n", 
        "Collection Value: $",runningValue);

       }// end method main

}//end class Inventory1

Here is inventory

public class Inv {//Begin DVD class

   // Declare instance variables
   String dvdTitle;// Declare title as string
   int dvdInStock;// Declare dvdInStock as float
   double dvdValue;// Declare dvdValue as float

   // constructor initializes DVD information
   public Inv(String title, int inStock, double value) { // Initialize (clear) instance variables here
      dvdTitle = title;
      dvdInStock = inStock;
      dvdValue = value;
      } // end constructor

   public String getdvdTitle() {// public method to get the DVD name
      return dvdTitle;}// end method getdvdTitle

   public int getdvdInStock() {// public method to retrieve the dvdInStock
      return dvdInStock;} // end method get dvdInStock

   public double getdvdValue() {// public method to retrieve the dvdValue
      return dvdValue;} // end method get dvdValue

   public double getdvdStockValue() {// public method to dvdStockValue
      return ( dvdValue * dvdInStock );}// end method get dvdStockValue

   } // end class Inv
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:10:05+00:00Added an answer on May 12, 2026 at 9:10 am

    Using the Comparator is easy and can be accomplished with something like this :

    public class InvSortByName implements Comparator<Inv>{
        public int compare(Inv o1, Inv o2) {
            return o1.getdvdTitle().compareTo(o2.getdvdTitle());
        }
    }
    

    I believe, based on the instructions provided and your code, that you may be misunderstanding the guidelines. It seems that there should be a main(…), an Inventory class, and a DVD/Product class. You currently only have part of that.

    You could replace the while loop in your main with a much cleaner and easier to use foreach. The instructions mention and provided an example of the for-each. This loop should be removed from the main based on the instructions provided.

    You should also review some of the basic Java best practices and naming conventions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok this is a homework questions, but I cannot find the answer anywhere, not
This is a homework task, but it's very simple. The task comes with a
Disclaimer: This is for a homework assignment, but the question is not regarding the
I know this sounds like a homework assignment, but it isn't. Lately I've been
This is homework, and this questions Extends this one So there is a button
This is not homework, I need this for my program :) I ask this
I have this homework problem where I need to use regex to remove every
This is my homework and I have thought a lot about it but I
For starters this is homework, I just really need help with a binary search
First, I'll admit this is homework but it has been around six years since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.