-
January 26th, 2005, 11:10 PM
#11
Hm, could you explain what the program is trying to do and post the ENTIRE code, including the public class statements and import statements. You seem to be using cs1 for input, am I correct?
-
January 26th, 2005, 11:18 PM
#12
Banned
i believe so, i just started this class in winter quater. i posted the code and what it is supposed to do on page one, help me clean that sucker up plz
-
January 26th, 2005, 11:47 PM
#13
Now I didn't bother checking if your code was correct (i.e. if it solved the problem) because I think that's something you should learn to do yourself. But I wanted to let you see how I would have laid it out. Basically, I normally divide my code into logical sections (i.e. a print asking for input and then an input statement, then a new line, etc.) I also keep if-then-else statements together, so applying that to your code I get:
Code:
import java.io.* ;
public class lab3_141
{
public static void main(String[] args)
{
int desk=200;
System.out.print("Enter the length of the desk : ");
int length=MyInput.readInt();
System.out.print("Enter the width of the desk : ");
int width=MyInput.readInt();
int area = (length * width);
System.out.println("Enter 1 for pine wood");
System.out.println("Enter 2 for oak wood");
System.out.println("Enter 3 for mahogany wood");
System.out.print("Enter the type of wood: ");
int wood = MyInput.readInt();
System.out.print("Enter the number of drawers: ");
int drawer= MyInput.readInt();
int drawers = drawer * 30;
if (area > 1000)
desk = desk+75;
if (area > 2000)
desk = desk+125;
else
desk=desk;
if (wood == 2)
desk=desk+100;
if (wood == 3)
desk=desk+150;
else
desk=desk;
System.out.println("The cost of your desk is: " + (desk+drawers));
}
}
Now when you're posting code to this forum, what you should do is arrange it with tabs and newlines, etc in a text editor, then paste it into the reply box and put "["code"]" before it and "["/code"]" after it (without the quotes. I put them in so that the tags would show up.
And remember, you don't need to copy my layout exactly, just arrange your code in sections so that it is easier to read and stick to however you arrange it with all your programs.
ac
-
January 26th, 2005, 11:47 PM
#14
Code:
import cs1.Keyboard;
public class ifStatementPractice {
public static void main(String args[]) {
int price = 200;
int width;
int length;
int surface;
int wood;
int drawers;
System.out.print("Enter table surface's width (inches): ");
width = Keyboard.readInt();
System.out.print("Enter table surface's length (inches): ");
length = Keyboard.readInt();
surface = width * length;
if((surface > 1000) && (surface < 2000)) {
price += 75;
}
if(surface > 2000) {
price += 125;
}
System.out.println("Wood Selection!");
System.out.println("1. Oak - $100");
System.out.println("2. Mahogany - $150");
System.out.println("3. Pine - $0");
System.out.print("Which type of wood: ");
wood = Keyboard.readInt();
switch(wood) {
case 1:
price += 100;
case 2:
price += 150;
case 3:
price += 0;
}
System.out.print("How many drawers would you like - $30/drawer: ");
drawers = Keyboard.readInt();
price += (drawers * 30);
System.out.println("Your new desk is going to cost: $" + price);
}
}
I haven't tested the code, but if you're gonna use it, you're gonna have to switch up the input statements since I use a third party package. If anyone notices anything wrong, drop me a line.
-
January 26th, 2005, 11:57 PM
#15
Banned
Wow thanks guys, that looks much better than mine and it helps to see whats going
Thanks for the input
-
January 27th, 2005, 12:00 AM
#16
No problem, if you have any questions, please do post, we'll be glad to help.
-
January 27th, 2005, 01:15 AM
#17
Banned
I put in my information needed and took out the switch ( so i understand if/else) and it now looks like this
Code:
import java.io.* ;
public class try
{
public static void main(String args[])
{
int price = 200;
int width;
int length;
int surface;
int wood;
int drawers;
System.out.print("Enter table surface's width (inches): ");
width = MyInput.readInt();
System.out.print("Enter table surface's length (inches): ");
length = MyInput.readInt();
surface = width * length;
if((surface > 1000) && (surface < 2000)) {
price += 75;
}
if(surface > 2000) {
price += 125;
}
System.out.println("Wood Selection!");
System.out.println("1. Oak - $100");
System.out.println("2. Mahogany - $150");
System.out.println("3. Pine - $0");
System.out.print("Which type of wood: ");
wood = MyInput.readInt();
if (wood == 1) {
price += 100;
}if (wood == 2) {
price += 150;
}if (wood == 3) {
price += 0;
}
System.out.print("How many drawers would you like - $30/drawer:$
drawers = MyInput.readInt();
price += (drawers * 30);
System.out.println("Your new desk is going to cost: $" + price);
}
}
I am gettin this error with it and im gettin a bit flustered.
Code:
try.java:3: <identifier> expected
public class try
^
try.java:52: '{' expected
^
2 errors
The way i read it is that I am missing a { or } but i counted and i have 7 { and 7 } lol so i dont know where to look next.
i dont understand about the switch just yet, (forgot to mention that we were supposed to only use if/else statements) but i re structured my program and it looks a bit nicer,
P.S. im not using TT's program for class, i already have one,and my profesor would know i didnt do it lol, im just tryin to understand some of the errors.
-
January 27th, 2005, 01:29 AM
#18
try is a Java keyword, you need a new name for your program.
System.out.print("How many drawers would you like - $30/drawer:$
That line appears to be slaughtered. No clue what you were trying to do. Look into it. =)
-
January 27th, 2005, 01:51 AM
#19
Banned
ah, that was it. and i fixed the other problem. Ok this is the final one as far as i know it compiles and runs
Code:
import java.io.* ;
public class lab3_141
{
public static void main(String[] args)
{
double area;
double wood;
double width;
double length;
double drawer;
double price = 200;
System.out.print("Enter the length of the desk : ");
length=MyInput.readInt();
System.out.print("Enter the width of the desk : ");
width=MyInput.readInt();
area = (length * width);
if (area > 1000)
price += 75;
if (area > 2000)
price += 125;
System.out.println("Wood Selection!");
System.out.println("1. Oak - $100");
System.out.println("2. Mahogany - $150");
System.out.println("3. Pine - $0");
System.out.print("Enter the type of wood: ");
wood = MyInput.readInt();
System.out.print("Enter the number of drawers: ");
drawer= MyInput.readInt();
if (wood == 2)
price += 100;
if (wood == 3)
price += 150;
else
price += 0;
price += (drawer * 30);
System.out.println("The cost of your desk is: $" + price );
}
}
Thank you for everythin, i actually learned alot expecially about cleaning up the program. I hope you take one final look at it and let me know if i can do anything else to better my technique.
-
January 27th, 2005, 02:04 AM
#20
Code:
if (area > 1000)
price += 75;
if (area > 2000)
price += 125;
Well I see that as a potential interpreting issue. When I originally coded the program I was under the assumption that if the surface area is over 1000 sq. inches that a fee of $75 would be added and if the surface area is over 2000 sq inches, that a $125 would be added and only a $125, not the $75 as well. With your current code, you will be charging the customer $200 extra. But I guess that is an interpretation issue, not coding. Otherwise it seems fine. Glad you learned something.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|