Jump to content

need code help, assignment due today

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
kris1976

kris1976

    Newbie

  • Members
  • PipPip
  • 16 posts
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.


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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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

#3
kris1976

kris1976

    Newbie

  • Members
  • PipPip
  • 16 posts
oh ok sorry, where is it suppose to go so I know for next time?

#4
kris1976

kris1976

    Newbie

  • Members
  • PipPip
  • 16 posts
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..

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts

kris1976 said:

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

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
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 :p) but this might be something.
// 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

Quote

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
(double)integer
does not work properly, use the doubleValue method: Integer.doubleValue Method
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

*/


#7
kris1976

kris1976

    Newbie

  • Members
  • PipPip
  • 16 posts
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...

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






#8
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
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.

#9
kris1976

kris1976

    Newbie

  • Members
  • PipPip
  • 16 posts
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.

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



[COLOR="Red"]import java.text.NumberFormat;[/COLOR]


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());


}



[COLOR="red"]import java.text.NumberFormat;[/COLOR]


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  


[COLOR="red"]import java.util.*;

import java.text.*;[/COLOR]


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

}


#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Are these separate files?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
kris1976

kris1976

    Newbie

  • Members
  • PipPip
  • 16 posts
no it's all one file...

#12
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
First of all, I would move all your import statements to the beginning of the file. It looks like you have duplicate statements. If you are still getting errors after that, post your errors along with the code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog