There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot browser bsod computer crash css dell desktop driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware help please hijackthis hjt hjt log install internet internet explorer itunes javascript keyboard laptop log malware monitor network networking openoffice outlook outlook 2003 outlook express password popups problem router seo slow sound sp3 spyware startup trojan usb video virtumonde virus vista vundo windows windows xp winxp wireless youtube
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: java programming help


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Justawind's Avatar
Member with 79 posts.
 
Join Date: Jul 2007
Location: York, Pa
Experience: Advanced
02-May-2008, 06:13 PM #1
Solved: java programming help
What I need to do is create a toString to show the genre of the DVDs, and also show a 5% restocking fee...Code below

import java.util.Arrays;

public class Inventory3 {

public static void main(String[] args) {
TypedDVD dvd = null;
Inventory inventory = new Inventory(); // Create a new inventory

System.out.println("\nNorman's DVD Inventory\n");

dvd = new TypedDVD(0, "Mother,Jugs and Speed", 1, 15.00f, "Comedy");
inventory.add(dvd);

dvd = new TypedDVD(1, "Clash of the Titans", 10, 12.49f, "Fiction");
inventory.add(dvd);

dvd = new TypedDVD(2, "300", 1, 1.00f, "History");
inventory.add(dvd);

dvd = new TypedDVD(3, "Full Metal Jacket", 2, 20.00f, "War");
inventory.add(dvd);

dvd = new TypedDVD(4, "Jarhead", 5, 5.00f, "War");
inventory.add(dvd);

inventory.display();

System.out.println("That's all, folks!\n");
} // end main method

} // end class Inventory3


class DVD {
private int productNumber;
private String title;
private int numCopies;
private float price;

DVD(int productNumber, String title, int numCopies, float price) {
this.productNumber = productNumber;
this.title = title;
this.numCopies = numCopies;
this.price = price;
}

public String toString() {
return String.format("ProdNum= %3d Title= %-25s numCopies= %3d price= $%6.2f value= $%7.2f",
productNumber, title, numCopies, price, value());
}

public float value() {
return numCopies * price;
}

} // end class DVD


class TypedDVD extends DVD {
private String genre;

TypedDVD(int productNumber, String title, int numCopies, float price, String genre) {
super(productNumber, title, numCopies, price);
this.genre = genre;

public String toString() {
return String.format("ProdNum= %3d Title= %-25s numCopies= %3d price= $%6.2f genre= %-25s value= $%7.2f",
productNumber, title, numCopies, price, genre, value());
}

public float restockingFee() {
return super.value() * 0.05f;
System.out.printf("\nThe restocking fee is $%.2f\n\n", value());


}

public float value() {
return super.value() + restockingFee();
}

} // end TypedDVD class


class Inventory {
private DVD[] dvds;
private int numDVDs;

Inventory() {
dvds = new DVD[10];
numDVDs = 0;
}

// Add a DVD to the collection
public void add(DVD dvd) {
dvds[numDVDs] = dvd;
++numDVDs;
}

public float value() {
float total = 0;
for (int i = 0; i < numDVDs; i++)
total += dvds[i].value();
return total;
}

public void display() {
System.out.println("There are " + numDVDs + " DVDs in the collection\n");
for (int i = 0; i < numDVDs; i++)
System.out.println(dvds[i]);
System.out.printf("\nTotal value of the collection is $%.2f\n\n", value());
}

} //end class Inventory
Chicon's Avatar
Computer Specs
Distinguished Member with 6,370 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
02-May-2008, 06:56 PM #2
Hi Justawind,

In the TypeDVD class, you may replace the following methods :
Code:

public String toString() {
    return String.format("ProdNum= %3d Title= %-25s numCopies= %3d price= $%6.2f genre= %-25s       value= $%7.2f", productNumber, title, numCopies, price, genre, value());
}

public float restockingFee() {
    return super.value() * 0.05f;
    System.out.printf("\nThe restocking fee is $%.2f\n\n", value());
}

public float value() {
    return super.value() + restockingFee();
}
by :
Code:

public String toString() {
    return genre;
}

public void restockingFee() {
    System.out.printf("\nThe restocking fee is $%.2f\n\n", value() * 1.05f);
}
The toString method of the TypeDVD class will override the toString method of DVD.
In the restockingFee method, value() is inherited from the DVD class, therefore, there's no need to 'super' it.
Justawind's Avatar
Member with 79 posts.
 
Join Date: Jul 2007
Location: York, Pa
Experience: Advanced
02-May-2008, 07:53 PM #3
Thanks, that did the trick.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,370 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
03-May-2008, 08:54 PM #4
You're welcome !
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 04:01 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.