13 Replies - 874 Views - Last Post: 15 July 2010 - 05:29 PM Rate Topic: -----

#1 MemoryLeak2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-July 10

Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 01:36 PM

Hello all. I'm a little new to this, so please forgive me for having just a single post... In all other subjects I have studied, I usually am the one to be giving help and tips, and now I'm in need of some. This stuff is not coming very easily. I promise as soon as I start to get better at programming I will come back and give others help :P. Thanks for reading my post and THANKS IN ADVANCE for the help.


As of now, my "Shipping Cost" calculator runs well on an infinite loop. Here's how I'd like the program to be, if possible: prompt the user for the Weight and the Distance 13 times, then, AFTER 13 prompts for user input of weight & distance, display the weight, the distance, and the cost to ship the item for each of their input, if possible in 13 different lines. Here's the current code as it is, on an infinite loop. The driver is below.
Ex:
//User prompts x 13
Weight: #.##
Distance: ###.#
Weight: ##
Distance: ##
.
.
.
//Report x 13
Weight: #.## Distance: ###.# Cost: ##.#
Weight: ## Distance: ## Cost: ##.#
.
.
.
public class ShippingCharges {
private double weight;
private double shippingDistance;    //class declarations
public ShippingCharges(){

}
//mutators and accessors (setters and getters)
public ShippingCharges(double weight, double shippingDistance) {

	this.weight = weight;
	
	this.shippingDistance = shippingDistance;
}

public double getShippingDistance() {
	return shippingDistance;
}

public void setShippingDistance(double shippingDistance) {
	
	this.shippingDistance = shippingDistance;
}

public double getWeight() {
	return weight;
}

public void setWeight(double weight) {

	this.weight = weight;
}
public double getShippingCharges(){
//Determines the rate by setting numPer500 = (distance/500.25), rounds up
	double numPer500=(double)((int)(shippingDistance/500.25)+1);
//series of if statements to determine the shipping charges
        if(shippingDistance==0) return 0.00*numPer500;
	if(weight==0) return 0.00*numPer500;
	if(weight<=2) return 1.10*numPer500;
	if(weight>2&&weight<=6) return 2.20*numPer500;
	if(weight>6&&weight<=10) return 3.70*numPer500;
	if(weight>10) return 4.80*numPer500;
	return 0;
}
//API- toString: "This object, (which is already a string!), is itself returned."
public String toString(){
	return "Weight of package="+weight+"kg. Shipping distance="+shippingDistance+"km. Cost to send the package is $"+getShippingCharges();
}
}



DRIVER

import java.util.Scanner;            //import scanner class
import java.text.NumberFormat;       //NumberFormat class to help parse numbers
import java.text.DecimalFormat;      //DecimalFormat class to help format decimals


public class TesterShippingCharges {
public static void main(String[] args){

        NumberFormat formatter = new DecimalFormat("#0.00");
        ShippingCharges num = new ShippingCharges();//num finds out the multiplication
	Scanner scanner = new Scanner(System.in);   // factor for the price

	ShippingCharges scan=new ShippingCharges();
	while (true){                               //Causes infinite loop
    System.out.print("Weight of package: ");                    // to continue prompting user.
	double weight = scanner.nextDouble();       //Retrieves user's input weight
        num.setWeight(weight);                      // and sets equal to weight.

	System.out.print("Distance to be sent: ");              //Prompts user for distance
	double distance = scanner.nextDouble();     //Retrieves user's input distance
        num.setShippingDistance(distance);          // and sets eual to distance

	scan.setWeight(weight);
	scan.setShippingDistance(distance);
        num.getShippingCharges();

	System.out.println(scan);
	if (weight==10000) break;
	}
}
}


Thanks for reading! Thanks even more if you could help with a fix!

This post has been edited by MemoryLeak2013: 15 July 2010 - 02:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Print a Report After, Instead of Case-by-Case

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 01:44 PM

Use an Array, ArrayList, or Vector to store your shipping charge objects as the user inputs the info

Then at the end, loop through and print out the data from your storage device

Welcome to DIC and please post your code in [code] tages next time :)
Was This Post Helpful? 1
  • +
  • -

#3 MemoryLeak2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-July 10

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 02:19 PM

Thank you; but we have not yet gone through Arrays, Array Lists, or Vectors.

So far the only way that I could think to execute this would be to add a counter some(where?) in there, delete the toString() part, and change the driver so it printed them at the end. How would I go about altering the code to do this? I've already turned this assignment in, but it would be nice to know how to do this in the future.
Any ideas?

I did see the BB formatting, but I must have missed [code] I'll see if I can edit it.
Was This Post Helpful? 0
  • +
  • -

#4 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 02:25 PM

Well obviously the least efficient way, i.e. creating 13 objects and prompting 13 times outside a loop and then printing out the objects at the end would work...

But other than that, I don't really see how you could do it without some sort of array. Were there any assignment-specific requirements, such as printing at the end, using loops, etc?
Was This Post Helpful? 0
  • +
  • -

#5 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 02:40 PM

you could put the whole thing in a forloop for(int i=0; i<13 //etc
then accumulate the cost
//forloop
// other code
cost+=num.getShippingCharges();
// end forloop
// System.out.println("Total cost: " + cost);


Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,542
  • Joined: 27-December 08

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 02:43 PM

Or just use a String variable to track the report. So after you update the existing Object with new information, add the toString() invocation result to an existing String variable to hold the entire report, then append an \n escape sequence for a new line.
Was This Post Helpful? 0
  • +
  • -

#7 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 02:44 PM

NoobKnight almost has it...

Make a String variable, assign your message like NoobKnight suggested, but add a line break "\n". You can still keep your while loop.
Was This Post Helpful? 0
  • +
  • -

#8 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 02:44 PM

View Postsample output, on 15 July 2010 - 02:36 PM, said:

Weight of package: 15
Distance to be sent: 25
Weight of package=15.0kg. Shipping distance=25.0km. Cost to send the package is $4.8
Weight of package: 45
Distance to be sent: 12
Weight of package=45.0kg. Shipping distance=12.0km. Cost to send the package is $4.8
Weight of package: 4
Distance to be sent: 1
Weight of package=4.0kg. Shipping distance=1.0km. Cost to send the package is $2.2
Weight of package: 150
Distance to be sent: 50
Weight of package=150.0kg. Shipping distance=50.0km. Cost to send the package is $4.8
Total cost: 16.6

Process finished with exit code 0

Was This Post Helpful? 1
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,542
  • Joined: 27-December 08

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 02:46 PM

NoobKnight, what point are you trying to make? No reason the OP can't print the String after the loop, generating the report in the format from the instructions. :)
Was This Post Helpful? 0
  • +
  • -

#10 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 03:05 PM

Interesting program.

More tweaks if interested.
You can remove the field scan, you don't need it, you can do everything with the num field.
Also at the end of the driver I added...
     System.out.println(num);   // this completes the remove of the scan field
     num.addTotalCost(num.getShippingCharges());   // I'm storing the totalcost in the class itself



ShippingCharges class.. I added the field and a getter and an accumulator
    private double totalCost;
//...

    public void addTotalCost(double x) {
         totalCost+=x;
    }
    public double getTotalCost() {
        return totalCost;
    }



The constructor is where I initialize the totalCost field.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,542
  • Joined: 27-December 08

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 03:18 PM

In that case, I would create an Item class, and have ShippingCharges encapsulate a List<Item> or Item[] to manage. Good suggestion, and definitely a more OO solution.
Was This Post Helpful? 0
  • +
  • -

#12 MemoryLeak2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-July 10

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 03:18 PM

[edit]WOW, I'm a lag. I multitasking between 3 or 4 different things, and by the time I finished writing this msg, there were like ... 7 helpful replies. THANK you everybody. I'm going to try some alterations that N00bKnight suggested.


No assignment-specific requirements. He gave us 13 different test cases, and the program gives the required output for all except 1. For some reason it doesn't format the price for a 10kg package going 1500 miles. The price comes out to be a really long double.


For our next assignment he has indicated the fact that we should use a loop. It is very similar to this one: get a user's speed and time traveled to calculate the distance. He wants us to design a class to store the speed and hours traveled, and a method that returns the DistanceTraveled. (see why I'm trying to just alter this code?)

This is the rather vague part: "It should display a report similar to the one shown here:"
Hours Distance Traveled
xx xxx
xx xxx
xx xxx
Also I would need to use try-catch-throw statements for illogical entries. (this shouldn't be that bad).

It would be very similar to the program I just wrote, except the 'report' part. Another diff is that we should continue prompting the user until they enter positive values, instead of reporting back to them that they traveled 0 distance.

I'm assuming I need to design a loop that can exit after a certain number of inputs, THEN display the report. I would like to learn how to do this by just altering the aforementioned code (if possible). The code above uses an infinite loop because in my opinion it was easier that way, and he didn't specify if or when he wanted the loop to end. Sorry if this is asking too much. I'm not going to check back here until I read the chapter and see if I can implement this on my own.

This post has been edited by MemoryLeak2013: 15 July 2010 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,542
  • Joined: 27-December 08

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 03:31 PM

Take a look at using String.format(), with formatting better explained here. More or less, to truncate all those decimal places String.format("%.2f", .2323424234); returns a String containing ".23" only, truncating the double to 2 decimal places.

To exit a loop, use the condition block. A for loop is set up as for(var-initialization; condition for coninuing; var-modification). So if you want the loop to stop when i == 10, say i != 10 in the condition section.
Was This Post Helpful? 1
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Print a Report After, Instead of Case-by-Case

Posted 15 July 2010 - 05:29 PM

View Postbcranger, on 15 July 2010 - 02:44 PM, said:

Use an Array, ArrayList, or Vector to store your shipping charge objects as the user inputs the info

Then at the end, loop through and print out the data from your storage device

Welcome to DIC and please post your code in [code] tages next time :)

@bcranger I already told you: if the post is obviously an homework and if it is obviously a basic Java program this is an assignment to teach how to use arrays. No need to offer ArrayList or Vector these students may not even know what an object is. So refrain your whillingness of showing your knowledge. (You were one of these not long time ago)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1