• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Programming Exercises :)

 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have tried the following questions in my reference book but i just couldnt figure out how to do it. my reference book doesn't include the answers, so i need help from you guys to tell me the answer. if possible please explain every steps thanks!
Question 1. Create a class named Sweets. Its main() method holds an integer variable named numberOfSweets to which you will assign a value. Create a method to which you pass numberOfSweets. The method displays the sweets in dozens. For example, 40 sweets is 3 dozens and 4 left over.
Question 2. Create a class named Shirt with data fields for collar size and sleeve length. Include a constructor that takes arguments for each field. Also Include a String class variable named material and initialize it to "cotton". Write a program TestShirt to instantiate 3 Shirt objects with different collar sizes and sleeve lengths, and then display all the data, including material, for each shirt.
 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for double posting. Anyway here is where i am stuck for Question 1...

/* Program name: Sweets.java
* Author name: Place your name here
* Description:
*/
import java.io.*;
public class Sweets {
public static void main(String[] args) throws IOException {
int sweets;
System.out.println("Enter number of sweets: ");
sweets = System.in.read();
numberOfSweets();

}
public int numberOfSweets() {
dozens = sweets / 12;
System.out.print("You have "+ dozens +" dozens!");
}
}

[ March 15, 2004: Message edited by: ayukawa madoka ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from the assignment:
Its main() method holds an integer variable named numberOfSweets to which you will assign a value.

so shouldn't your "int sweets" really be "int numberOfSweets"? and i'd take the second part to just mean i can set the value myself in the code...
int numberOfSweets = 73;//or whatever you want.
you then need a method (called something else, like "displayDozens"). this will take an int parameter. it doesn't need to return anything.
the logic in your method is right, but not complete. it won't tell you how many you have leftover. to do that, you need the modulus operator.
feel free to post more questions once you try and work these suggestions out.
 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i completed the program...


To reduce the number of lines in this program, instead of...

I did...

I already declared the variables but it won't work. When compile it says

(same goes to leftOver)

[ March 16, 2004: Message edited by: ayukawa madoka ]
[ March 16, 2004: Message edited by: ayukawa madoka ]
[ March 16, 2004: Message edited by: ayukawa madoka ]
[ March 16, 2004: Message edited by: ayukawa madoka ]
 
Howard Ting
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ayukawa madoka:
I did...

I already declared the variables but it won't work. When compile it says

(same goes to leftOver)


Would you check your code again? That shouldn't be "dozens / 12" and truly is "nOS / 12."
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howard is right... you don't want

you're getting the error because local variables aren't automatically initialized. if you had
int dozens = 0, leftOver = 0;
your code would compile. you'd get the wrong result, but it would compile. with your change to make life simpler, you don't even need these two variables.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To reduce the number of lines in this program, instead of...
I did...
To be very clear about what Howard has suggested, these two blocks of code are not equivalent.
Before I forget, it should not be your goal to "reduce the number of lines" in your program. When writing code, you should strive for clarity, not brevity.
Now, concerning your broken code replacements, realize that in your original example, you were printing the value of dozens[i/], which had been assigned the value of [i]nOS / 12. In the changed version, you're printing a different value, the value of dozens / 12. Do you see now that nOS / 12 and dozens / 12 are not the same (unless of course, you h0ad assigned dozens the value of nOS in the changed code)?
If you understand this first part better, then the second part of your examples should become a little clearer.
[ March 16, 2004: Message edited by: Dirk Schreckmann ]
 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i did a mistake, yes it was nOS/12 and nOS%12 instead of dozens/12 and leftOver/12
please forgive my carelessness.
mistake corrected. program able to compile and execute.
i have another problem. now, instead of assigning the numberOfSweets to value 30, i used the System.in.read() to allow the user to input whatever figure they want. the program was able to compile and execute but the problem is the output was inaccurate. let say the user input the value of 30, then the output for dozens is 4 and the output for leftOver is 3 instead of dozens=6 and leftOver=2. why do this happen? is there anything wrong with my program? please help me!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic