Results 1 to 9 of 9
- 06-05-2011, 04:22 PM #1
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Beginner trying to write Java code, has issue w/ printing result and 2 decimals
I'm taking a Java class which I am really liking so far. So I have this problem I am working on now.
A. Create a class name purchase. Each purchase. Contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set() method for the sale amount, calculate the sales tax as 5% of the sale amount. Also include a display method that displays a purchase's details. Save the file as purchase.java
The issue I am having is the other two methods are not printing (sale2 and sale3). then on top of that I cannot get the final price to print 2 decimal places. It does not seem double or int will work, so what other options do I have?
Thanks for the ideas folks.
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
int invoice1 = 25;
int invoice2 = 28;
double amtSale1 = 18.99;
double amtSale2 = 39.59;
double amtSale3 = 75.89;
double tax = 1.05;
sale1 (invoice1, amtSale1, tax);
}
public static int sale1(int invoice1, double amtSale1, double tax)
{
double total = tax * amtSale1;
System.out.println("The invoice number is " + invoice1 + ". And the total with 5% tax is $" + total);
double newAmount;
newAmount = total;
return (int) newAmount;
}
public static int sale2(int invoice2, double amtSale2, double tax)
{
double total = tax * amtSale2;
System.out.println("The invoice number is " + invoice2 + ". And the total with 5% tax is $" + total);
double newAmount;
newAmount = total;
return (int) newAmount;
}
public static int sale3(int invoice3, double amtSale3, double tax)
{
double total = tax * amtSale3;
System.out.println("The invoice number is " + invoice3 + ". And the total with 5% tax is $" + total);
double newAmount;
newAmount = total;
return (int) newAmount;
}
}
- 06-05-2011, 04:58 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Please use code tags to make your code more readable, you can edit them in. To do this type
[code]
YOUR CODE HERE
[/code]
I read your post more thoroughly and have some information. First you need to alter your code quite a bit. As it stands you are not taking advantage of the oopness.
A class should be a blueprint for a type of item, it should have instance variables, and methods that work on the instance variables. The main method should create instances of the class and call methods on it. What you have now is not quite what the assignment is asking for.
I suggest you scrap it and start over, thinking of the class as a representation of a single item. Give it instance variables(the assignment states what they should be) and methods(also stated in the assignment. You should only have one static method in this entire class(main). Try doing this and post your progress with any challenges you run into. If you need help, consider reading the java tutorials, here is a link
Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
Also, consultyour text book and check out there explanations and examples of using and creating classes.Last edited by sunde887; 06-05-2011 at 05:05 PM.
- 06-05-2011, 05:25 PM #3
I'll take the simple one:
Look at the printf() method. It allows formatting of output.to print 2 decimal places
- 06-05-2011, 07:41 PM #4
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Revision
OK here is what I have now. This is all so confusing, but I'm trying something different with the class now. I think I see what you mean. I just started this class last week, so it is taking a little to sink in. I hope this is kinda right.
I am going to look at the printf() method for getting the two decimal places, but there is an extra itemDetails window that pops up for some reason. I'm still trying to look in to that.
Any critiques are welcomed. Thank you.
Java Code:import javax.swing.JOptionPane; public class Purchase { public static void main(String[] args) { double totalWithTax; String invoiceNum, itemDetails, itemPrice, totalPrice; final double TAX = 1.05; invoiceNum = JOptionPane.showInputDialog(null, "Enter invoice number", "Invoice Number", JOptionPane.INFORMATION_MESSAGE); itemDetails = JOptionPane.showInputDialog(null, "Enter item details", "Item Details", JOptionPane.INFORMATION_MESSAGE); itemPrice = JOptionPane.showInputDialog(null, "Enter item price", "Price of item", JOptionPane.INFORMATION_MESSAGE); totalWithTax = Double.parseDouble(itemPrice) * TAX; totalPrice = JOptionPane.showInputDialog(null, "Enter item details", "Item Details", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "The invoice number is " + invoiceNum + ". The total for the " + itemDetails + " with 5% tax is " +totalWithTax); } }
- 06-05-2011, 08:03 PM #5
Have you looked at the code you posted to see where it is coming from? Its not very much code so you should be able to see it.there is an extra itemDetails window that pops up for some reason
- 06-05-2011, 08:22 PM #6
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Ok. I'll play around some more. I know it's gotta be easy. thanks all.
- 06-06-2011, 12:40 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also, you aren't using oop yet. In the main method you should be able to do something like this
Each of those objects(p1-pn) is a unique object. They all allow you to manage the data in the class differently. Changes to p1 should do nothing to p2, and changes to p2 should do nothing to any other. The changes are basically isolated to one object.Java Code:public static void main(String[] args){ Purchase p1 = new Purchase(); Purchase p2 = new Purchase(); Purchase p3 = new Purchase(); ... Purchase pn = new Purchase(); }
The general shape of a class should look something like this
You can also include the main method in the class, but you don't need it. Although the class may be a bit challenging to test without a main method.Java Code:public class SomeClass{ //instance variables //represents the unique state of an object int x; String a; ... //more instance variables if necessary //methods //represents a method which allows you to modify //the instance variables(or state) of some particular //instance of an object public void setX(int x){ this.x = x; } public void setA(String a){ this.a = a; } public String toString(){ return "A is: " + a + "\nX is: " + x; } }
edit: This post may be useful to you
Need Serious Help on Odometer exerciseLast edited by sunde887; 06-06-2011 at 12:42 AM.
- 06-06-2011, 01:34 AM #8
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
To be honest I have no idea what "oop" is. I'm thoroughly confused. It's been tough, and I know this is an easy assignment. This stuff is frustrating!!!!!
- 06-06-2011, 02:27 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
In this context it refers to a way of writing code using classes that represent particular types of thing.To be honest I have no idea what "oop" is.
In your case you are dealing with purchases and told to write a Purchase class to represent a purchase. It is good to have a mental idea of what a purchase is: a receipt might do. Something concrete. The receipt contains an invoice number and the amount of the sale, and these things *define* the purchase in a sense. It also contains a sales tax number, but we know that the sales tax is always the same so the sales tax doesn't really tell us anything more about that particular purchase.
I won't write the Purchase class. But I'll write another simple class: a Person class that represents the type of thing a person is. A person has a name and an age and for my purposes these things define a person. There are other qualities of the person like whether they are over 18 that might be calculated.
One thing to notice is that although this class describes in code what I described above in words a person to be, there is no main() method and there are no dialogs or other input and output. This is intentional. Creating a person, giving him/her a name and saying things about that person are not properties or behaviours of a person. (More likely they are properties and behaviours of a newly created person's parents.)Java Code:/** * A person is a thing with a name and an age. They can provide a description * of themselves including a statement of whether they are over 18. */ public class Person { private String name; private int age; private boolean isOver18; /* Creates a person with their details unset. */ public Person() {} /** Sets the name of the person to a given value. */ public void setName(String name) { this.name = name; } /** Sets the age of the person to a given value. */ public void setAge(int age) { this.age = age; isOver18 = age > 18; } /** Returns a string describing the person. */ public String describe() { return name + " aged " + age + ". Over 18 = " + isOver18; } }
As noted above without these things the class is hard to test. That can be done (if it is done at all - it wasn't part of your original post) in a separate class.
Possibly that will give you something to think about.Java Code:public class Main { public static void main(String[] args) { Person p = new Person(); p.setName("Jack"); p.setAge(17); System.out.println(p.describe()); p = new Person(); p.setName("Jill"); p.setAge(21); System.out.println(p.describe()); } }
Similar Threads
-
Help in printing the result of the for loop
By maas in forum Java ServletReplies: 1Last Post: 05-12-2011, 09:30 AM -
I code a binary division in java, but the result is not desirable.
By amityadav9314 in forum New To JavaReplies: 1Last Post: 03-09-2011, 03:45 PM -
Plzz Help...Issue when doing Text Printing from Java to Canon iR1018, iR1020
By rameshZSL in forum Advanced JavaReplies: 1Last Post: 01-21-2011, 11:46 AM -
Issue compiling Java Code
By AggressiveFish in forum Java AppletsReplies: 4Last Post: 01-04-2011, 09:10 PM -
New to java and does not know how to write the code ...
By poisson in forum New To JavaReplies: 57Last Post: 07-15-2010, 04:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks