Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: need code help, assignment due today

  1. #1
    kris1976 is offline Newbie
    Join Date
    May 2009
    Posts
    14
    Rep Power
    0

    Question need code help, assignment due today

    Not sure why my restocking fee is not displaying, there are no errors, it runs and displays Genre but the restocking fee does not display... ???
    Assignment: Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the CD subclass, you could use CD genre, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
     Modify the output to display this additional feature you have chosen and the restocking fee.


    Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class InventoryProgramPart3 {
    
    // main method begins program execution
    public static void main(String args[]) {
    
    // create Scanner to obtain input
    Scanner input = new Scanner( System.in );
    
    // display welcome message
    System.out.println( "Welcome to Felt's Inventory!" );
    
    // cd
    
    cd[] cd = new cd[100]; // array of 100 cd
    
    cd GeorgeStraight = new cd(1, "George Straight", 5, 14.00);
    cd CarrieUnderwood = new cd(2, "Carrie Underwood", 4, 13.99);
    cd FaithHill = new cd(3, "Faith Hill", 6, 15.00);
    cd JoshTurner = new cd(4, "Josh Turner", 8, 12.99);
    cd GarthBrooks = new cd(5, "Garth Brooks", 3, 11.99);
    
    // display inventory one at a time
    
    GeorgeStraight.showInventory();
    CarrieUnderwood.showInventory();
    FaithHill.showInventory();
    JoshTurner.showInventory();
    GarthBrooks.showInventory();
    
    // sort cds by name
    for ( int i = 0; i < args.length; i++ )
    System.out.println( args[i] + "," );
    
    double array[] = { 70.00, 55.96, 90.00, 103.92, 35.97 };
    double total = 0;
    
    // add each value to total
    for ( int counter = 0; counter < array.length; counter++)
    	total += array[ counter ];
    System.out.printf( "\nTotal inventory value is: $%.2f\n", total );
    
    System.out.println( "\nThank you for using Felt's Inventory\n" );
    
    } // end main method
     
    } // end class InventoryProgramPart3
    
    class cd
    {
    
    public int cdItem;
    public String cdArtist;
    public int cdUnits;
    public double cdPrice;
    
    // set cd Item
    public void setCdItem(int item) {
    	this.cdItem = item;
    } // end method set Cd Item
    
    //return cd Item
    public int getCdItem() {
    	return cdItem;
    } // end method get Cd Item
    
    // set cd Artist
    public void setCdArtist(String artist) {
    	this.cdArtist = artist;
    } // end method set Cd Artist
    
    //return cd Artist
    public String getCdArtist() {
    	return cdArtist;
    } // end method get cd Artist
    
    // set cd Units
    public void setCdUnits(int units) {
    	this.cdUnits = units;
    } // end method set Cd Units
     
    //return cd in stock
    public int getCdUnits() {
    return cdUnits;
    } //end method get Cd Units
    
    public void setCdPrice(double price) {
    	this.cdPrice = price;
    } //end method set Cd Price
    
    //return cd Price
    public double getCdPrice() {
    	return cdPrice;
    } //end method get Cd Price
    
    // calculate value of cd inventory
    public double getValue()
    {
    return cdUnits * cdPrice;
    }// end method value of cd inventory
    
    // constructor
    cd( int item, String artist, int units, double price )
    {
    cdItem = item;
    cdArtist = artist;
    cdUnits = units;
    cdPrice = price;
    
    } // end constructor
    
    // display inventory
    public void showInventory()
    {
    	System.out.println(); // blank line
    
    System.out.println( "Cd Item: "+cdItem );
    System.out.println( "Cd Artist: "+cdArtist );
    System.out.println( "Units in Stock: "+cdUnits );
    System.out.printf( "Unit Price: $%.2f", cdPrice );
     
    genre cd = new genre
    	( 1, "George Straight", 5, 14.00, "Country" );
    
    System.out.println( "\nGenre: "+cd.getGenre() );
    
    // value() method and display value
    System.out.printf( "\nInventory value of "+cdArtist+ " is = $%.2f\n", getValue() );
    
    } // end display inventory
    
    } // end class cd
    
    class genre extends cd
    {
    // holds cd genre
    private String cdGenre;
    
    // constructor
    genre( int item, String artist, int units, double price, String genre )
    {
    	super( item, artist, units, price );
    	cdGenre = genre;
    } // end constructor
    
    // set cd genre
    public void setGenre( String genre )
    {
    	this.cdGenre = genre;
    } // end method set cd genre
    
    // return cd genre
    public String getGenre()
    {
    	return cdGenre;
    } // end method get cd genre
    
    // add 5% restocking fee
    public double getValue()
    {
    	return super.getValue() * 1.05;
    } // end method return cd genre
    
    // calculate restocking fee
    public double getRestockingFee()
    {
    	return super.getValue() * .05;
    } // end method caluclate restocking fee
    
    // return String cdGenre
    public String toString()
    {
    String formatString = "Genre: %s";
    formatString += "Restocking Fee: $%.2f";
    formatString = String.format( formatString, cdGenre, super.getValue() * 0.05 );
    return( formatString + super.toString() );
    } // end toString()
    
    // display inventory
    public void showInventory()
    {
    	super.showInventory();
    System.out.println( toString() );
    
    // Display value plus restocking fee
    System.out.printf( "\nInventory value of "+cdArtist+ " is = $%.2f\n",
    getRestockingFee() );
    
    } // end method display inventory
    
    } // end class genre

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: need code help, assignment due today

    Kris, please do not post Java questions in the JavaScript tutorials forum. Moved to the correct location.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    kris1976 is offline Newbie
    Join Date
    May 2009
    Posts
    14
    Rep Power
    0

    Re: need code help, assignment due today

    oh ok sorry, where is it suppose to go so I know for next time?

  5. #4
    kris1976 is offline Newbie
    Join Date
    May 2009
    Posts
    14
    Rep Power
    0

    Re: need code help, assignment due today

    I need to turn this assignment in tonight (Sun 6-21), can anyone help me figure out why the restocking fee is not displaying??? Any help would be greatly appreciated..

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: need code help, assignment due today

    Quote Originally Posted by kris1976 View Post
    oh ok sorry, where is it suppose to go so I know for next time?
    Software development -> Java Help
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,881
    Rep Power
    24

    Re: need code help, assignment due today

    Am i too late?
    I'm not on a computer with java so just looking at the Tostring method without testing.
    So i will list some things that COULD be wrong.

    It's been quite a while since i've used those %d, %f things (since C++). Are you sure the double symbol = %f? seems like float to me (i can be wrong tho). Maybe try %lf.

    then, the super.getValue in the tostring allready contains "*0.05". After calling the method you do "*0.05" again. I don't know if this is intended.

    Then i'm not sure (again ) but this might be something.
    Code:
    // calculate value of cd inventory
    public double getValue()
    {
    return cdUnits * cdPrice;
    }// end method value of cd inventory
    from class cd. There you want to multiply an integer with a double.
    quote from Java:Tutorials:Variables - GPWiki
    The Java compiler is quite picky when it comes to types. Ints can only be multiplied by other ints, floats with floats, doubles with double and so on
    So i would cast the int into a double first. if
    Code:
    (double)integer
    does not work properly, use the doubleValue method: Integer.doubleValue Method
    Code:
    public class MyClass
    {
        public static void main(String[] args)
        {
            Integer i = new Integer(123);
    
            System.out.println("The double value is: " + i.doubleValue());
       }
    }
    /*
    Output:
    The double value is: 123.0
    */

  8. #7
    kris1976 is offline Newbie
    Join Date
    May 2009
    Posts
    14
    Rep Power
    0

    Re: need code help, assignment due today

    well didn't end up getting very good grade, professor said i didn't use an array to store the info and didn't use a sort method, also said there is no method to determine amount of entire inventory... he said I need to fix this before doing part 4 which is due tomorrow. pLease help : ( I so afraid I am going to fail this class...

    Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class InventoryProgramPart3 {
    
    // main method begins program execution
    public static void main(String args[]) {
    
    // create Scanner to obtain input
    Scanner input = new Scanner( System.in );
    
    // display welcome message
    System.out.println( "Welcome to Felt's Inventory!" );
    
    // cd
    
    cd[] cd = new cd[100]; // array of 100 cd
    
    cd GeorgeStraight = new cd(1, "George Straight", 5, 14.00);
    cd CarrieUnderwood = new cd(2, "Carrie Underwood", 4, 13.99);
    cd FaithHill = new cd(3, "Faith Hill", 6, 15.00);
    cd JoshTurner = new cd(4, "Josh Turner", 8, 12.99);
    cd GarthBrooks = new cd(5, "Garth Brooks", 3, 11.99);
    
    // display inventory one at a time
    
    GeorgeStraight.showInventory();
    CarrieUnderwood.showInventory();
    FaithHill.showInventory();
    JoshTurner.showInventory();
    GarthBrooks.showInventory();
    
    // sort cds by name
    for ( int i = 0; i < args.length; i++ )
    System.out.println( args[i] + "," );
    
    double array[] = { 70.00, 55.96, 90.00, 103.92, 35.97 };
    double total = 0;
    
    // add each value to total
    for ( int counter = 0; counter < array.length; counter++)
    	total += array[ counter ];
    System.out.printf( "\nTotal inventory value is: $%.2f\n", total );
    
    System.out.println( "\nThank you for using Felt's Inventory\n" );
    
    } // end main method
     
    } // end class InventoryProgramPart3
    
    class cd
    {
    
    public int cdItem;
    public String cdArtist;
    public int cdUnits;
    public double cdPrice;
    
    // set cd Item
    public void setCdItem(int item) {
    	this.cdItem = item;
    } // end method set Cd Item
    
    //return cd Item
    public int getCdItem() {
    	return cdItem;
    } // end method get Cd Item
    
    // set cd Artist
    public void setCdArtist(String artist) {
    	this.cdArtist = artist;
    } // end method set Cd Artist
    
    //return cd Artist
    public String getCdArtist() {
    	return cdArtist;
    } // end method get cd Artist
    
    // set cd Units
    public void setCdUnits(int units) {
    	this.cdUnits = units;
    } // end method set Cd Units
     
    //return cd in stock
    public int getCdUnits() {
    return cdUnits;
    } //end method get Cd Units
    
    public void setCdPrice(double price) {
    	this.cdPrice = price;
    } //end method set Cd Price
    
    //return cd Price
    public double getCdPrice() {
    	return cdPrice;
    } //end method get Cd Price
    
    // calculate value of cd inventory
    public double getValue()
    {
    return cdUnits * cdPrice;
    }// end method value of cd inventory
    
    // constructor
    cd( int item, String artist, int units, double price )
    {
    cdItem = item;
    cdArtist = artist;
    cdUnits = units;
    cdPrice = price;
    
    } // end constructor
    
    // display inventory
    public void showInventory()
    {
    	System.out.println(); // blank line
    
    System.out.println( "Cd Item: "+cdItem );
    System.out.println( "Cd Artist: "+cdArtist );
    System.out.println( "Units in Stock: "+cdUnits );
    System.out.printf( "Unit Price: $%.2f", cdPrice );
     
    genre cd = new genre
    	( 1, "George Straight", 5, 14.00, "Country" );
    
    System.out.println( "\nGenre: "+cd.getGenre() );
    
    // value() method and display value
    System.out.printf( "\nInventory value of "+cdArtist+ " is = $%.2f\n", getValue() );
    
    } // end display inventory
    
    } // end class cd
    
    class genre extends cd
    {
    // holds cd genre
    private String cdGenre;
    
    // constructor
    genre( int item, String artist, int units, double price, String genre )
    {
    	super( item, artist, units, price );
    	cdGenre = genre;
    } // end constructor
    
    // set cd genre
    public void setGenre( String genre )
    {
    	this.cdGenre = genre;
    } // end method set cd genre
    
    // return cd genre
    public String getGenre()
    {
    	return cdGenre;
    } // end method get cd genre
    
    // add 5% restocking fee
    public double getValue()
    {
    	return super.getValue() * 1.05;
    } // end method return cd genre
    
    // calculate restocking fee
    public double getRestockingFee()
    {
    	return super.getValue() * .05;
    } // end method caluclate restocking fee
    
    // return String cdGenre
    public String toString()
    {
    String formatString = "Genre: %s";
    formatString += "Restocking Fee: $%.2f";
    formatString = String.format( formatString, cdGenre, super.getValue() * 0.05 );
    return( formatString + super.toString() );
    } // end toString()
    
    // display inventory
    public void showInventory()
    {
    	super.showInventory();
    System.out.println( toString() );
    
    // Display value plus restocking fee
    System.out.printf( "\nInventory value of "+cdArtist+ " is = $%.2f\n",
    getRestockingFee() );
    
    } // end method display inventory
    
    } // end class genre

  9. #8
    cdg10620's Avatar
    cdg10620 is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Texas
    Posts
    387
    Blog Entries
    3
    Rep Power
    12

    Re: need code help, assignment due today

    I know what it's like to be in class and struggle with a problem. I am not going to solve this for you but I will give you a few pointers. Do some research on data structures and using arrays in Java. There are many sort methods you can use for this. Also, one of the best ways to learn programming is to sit down and work through the problem. Not post to a forum and expect someone to answer it for you.

    I'm not ripping on you by any means. Just trying to be honest and help you learn. There is no better teacher than an IDE and a debugger. Keep working through it. It may take hours and hours to work through a piece of code, but when you are done you'll know what you're doing.

    Another good motto to live by... "When in doubt... Google..."

    Google "Data Structures Array Java" and you'll find hundreds of documents, tutorial websites, and forums. There is plenty of information. You just have to dig a little bit.

  10. #9
    kris1976 is offline Newbie
    Join Date
    May 2009
    Posts
    14
    Rep Power
    0

    Re: need code help, assignment due today

    Desperately needing help, got errors on the lines in red, must be same thing, it's where I import says class interface or enum expect at the 1st one, the others say illegal start of type and ; expected... I am at my wits end. I am sorry for being a big pain, I just need to get through this class, its the last programming class. Thanks in advance for any help.

    Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class InventoryProgramPart3 {
    
    // main method begins program execution
    public static void main(String args[]) {
    
    // create Scanner to obtain input
    Scanner input = new Scanner( System.in );
    
    // display welcome message
    System.out.println( "Welcome to Felt's Inventory!" );
    
    // cd
    
    cd[] cd = new cd[100]; // array of 100 cd
    
    cd GeorgeStraight = new cd(1, "George Straight", 5, 14.00);
    cd CarrieUnderwood = new cd(2, "Carrie Underwood", 4, 13.99);
    cd FaithHill = new cd(3, "Faith Hill", 6, 15.00);
    cd JoshTurner = new cd(4, "Josh Turner", 8, 12.99);
    cd GarthBrooks = new cd(5, "Garth Brooks", 3, 11.99);
    
    // display inventory one at a time
    
    GeorgeStraight.showInventory(5);
    CarrieUnderwood.showInventory(4);
    FaithHill.showInventory(6);
    JoshTurner.showInventory(8);
    GarthBrooks.showInventory(3);
    
    // sort cds by name
    for ( int i = 0; i < args.length; i++ )
    System.out.println( args[i] + "," );
    
    double array[] = { 70.00, 55.96, 90.00, 103.92, 35.97 };
    double total = 0;
    
    // add each value to total
    for ( int counter = 0; counter < array.length; counter++)
    	total += array[ counter ];
    System.out.printf( "\nTotal inventory value is: $%.2f\n", total );
    
    System.out.println( "\nThank you for using Felt's Inventory\n" );
    
    } // end main method
     
    } // end class InventoryProgramPart3
    
    
    import java.text.NumberFormat;
    
    public class Stock {
    
    private String cdArtist;
    private int cdItem;
    private int cdUnits;
    private double cdPrice;
    
    public Stock()
    {
    
    this.cdArtist = "";
    this.cdItem = 0;
    this.cdUnits = 0;
    this.cdPrice = 0;
    
    }
    
    public Stock(Sring artist, int item, int units, double price)
    {
    
    this.cdArtist = artist;
    this.cdItem = item;
    this.cdUnits = units;
    this.cdPrice = price;
    }
    
    public void setCdArtist(String artist)
    {
    	this.cdArtist = artist;
    }
    
    public void setCdPrice(double cdPrice)
    {
    	this.cdPrice = cdPrice;
    }
    
    public void setCdUnits (int units)
    {
    this.cdUnits = units;
    }
    
    public void setCdItem(int item)
    {
    this.cdItem = item;
    }
    
    public String getCdArtist()
    {
    	return this.cdArtist;
    }
    
    public int getCdItem()
    {
    	return this.cdItem;
    }
    
    public double getCdPrice()
    {
    	return this.cdPrice;
    }
    
    public int getCdUnits()
    {
    	return this.cdUnits;
    }
    
    public double calculateTotalCdValue()
    {
    	return getCdUnits()* getCdPrice();
    }
    
    public String toString()
    {
                NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
            return"\nCdArtist: "+getCdArtist() + "\nCd Item: "+getCdItem()+"\nCd Price : "
                    +currencyFormat.format(getCdPrice())
                    +"\nCd Units: "+getCdUnits()
                    +"\nValue of Inventory: "+currencyFormat.format(this.calculateTotalCdValue());
    
    }
    
    
    import java.text.NumberFormat;
    
    public class StockSubClass extends Stock
    {
    
    	private String CdGenre;
    	public StockSubClass()
    {
    	super();
    	CdGenre = "";
    }
    
    public void setCdGenre(String genre)
    {
    	this.CdGenre = genre;
    }
    
    public String getCdGenre()
    {
    	return this.CdGenre;
    }
    
    public double calculateRestockFee()
    {
    	return (super.getCdPrice() * .05);
    }
    
    // total cd inventory value
    
    public double calculateInventoryValue()
    {
    	return (super.getCdPrice()*super.getCdUnits() + calculateRestockFee() );
    }
    
    // total value of all inventory
            public static double getTotalInventoryValue(StockSubClass [] items)
    {
                double tot = 0.0;
                
                for(int j = 0; j < items.length; j++)
                {
                    tot += items[j].calculateInventoryValue();
                }
                return tot;
    }
            
    
            
    // override the toString function to display a single Item @Override
    
    public String toString()
    {
                NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
            return"\nCd Artist: "+super.getCdArtist() + "\nCd Item: "+super.getCdItem()+"\nCd Price : "
                    +currencyFormat.format(super.getCdPrice())+"\nRestock Fee : "
                    +currencyFormat.format(calculateRestockFee())+"\nCd Units: "+super.getCdUnits()+"\nGenre: "
                    +getCdGenre()+"\nInventory Value: "+currencyFormat.format(this.calculateInventoryValue()) +"\n\n";
           
    } // end method inventory
    
    } // end class Stock  
    
    import java.util.*;
    import java.text.*;
    
    public class StockInput
    {
    
    private StockSubClass [] cds;
    
    // constructor
    
    public StockInput (int numberOfCds)
    {
    
    cds = new StockSubclass[numberOfCds];
    for(int i = 0; i < numberOfCds; i++)
    cds[i] = new StockSubClass();
    }
    
    public StockSubClass[] inputCds()
    {
    
    cds[0].setCdArtist("George Straight");
    cds[0].setCdUnits(5);
    cds[0].setCdPrice(14.00);
    cds[0].setCdItem(1);
    cds[0].setCdGenre("Country");
    
    cds[1].setCdArtist("Carrie Underwood");
    cds[1].setCdUnits(4);
    cds[1].setCdPrice(13.99);
    cds[1].setCdItem(2);
    cds[1].setCdGenre("Country");
    
    cds[2].setCdArtist("Faith Hill");
    cds[2].setCdUnits(6);
    cds[2].setCdPrice(15.00);
    cds[2].setCdItem(3);
    cds[2].setCdGenre("Country");
    
    cds[3].setCdArtist("Josh Turner");
    cds[3].setCdUnits(8);
    cds[3].setCdPrice(12.99);
    cds[3].setCdItem(4);
    cds[3].setCdGenre("Country");
    
    cds[4].setCdArtist("Garth Brooks");
    cds[4].setCdUnits(3);
    cds[4].setCdPrice(11.99);
    cds[4].setCdItem(5);
    cds[4].setCdGenre("Country");
    
    return (cds);
    }
    
    // Sort the Cd array by artist
    public StockSubClass [] sortByCdArtist;
    {
    
    StockSubClass[] sorted = new StockSubClass[cds.length];
    String [] artists = new String[cds.length];
    for(int i = 0; i< cds.length; i++)
    artists[i] = cds[i].getCdArtist();
    Arrays.sort(artists);
    int index = -1;
    for(int i = 0; i< artists.length; i++)
    {
    
    index = this.searchByArtist(artists[i]);
    if (index!= -1)
    	soretd[i] = cds[index];
    }
    this.cds = sorted;
    return (sorted);
    }
    
    // search a Cd by artist and return the cd index
    private int searchByArtist(String artist)
    {
    for(int i = 0; i < cds.length; i++)
    	if(artist.equals(cds[i].getCdArtist()))
    	return i;
    return -1;
    }
    
    }// end class StockInput
    }

  11. #10
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: need code help, assignment due today

    Are these separate files?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. add code for today date in my php class/function
    By newphpcoder in forum PHP Development
    Replies: 8
    Last Post: 05-18-2011, 09:05 PM
  2. Need Help...Java assignment due today
    By lostgoat in forum Java Help
    Replies: 1
    Last Post: 06-22-2009, 09:52 AM
  3. What I bought today ?
    By Jaan in forum The Lounge
    Replies: 15
    Last Post: 02-04-2009, 11:12 PM
  4. GuRu - Today or when?
    By Turk4n in forum The Lounge
    Replies: 51
    Last Post: 01-07-2009, 10:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts