*
The moose likes Beginning Java and the fly likes Programming Exercises :) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Programming Exercises :)" Watch "Programming Exercises :)" New topic
Author

Programming Exercises :)

Ayukawa Madoka
Greenhorn

Joined: Feb 28, 2004
Posts: 27
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.


hello everyone i'm new here.
Ayukawa Madoka
Greenhorn

Joined: Feb 28, 2004
Posts: 27
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

Joined: Oct 02, 2003
Posts: 11169
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ayukawa Madoka
Greenhorn

Joined: Feb 28, 2004
Posts: 27
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

Joined: Feb 17, 2004
Posts: 19
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

Joined: Oct 02, 2003
Posts: 11169
    
  16

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

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ayukawa Madoka
Greenhorn

Joined: Feb 28, 2004
Posts: 27
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Programming Exercises :)
 
Similar Threads
Serialization: Non serialized class issue
HI please Help with. GC Kathy and Bert Book
Problem with Object Serialization
Question about GC and Objects
Comprehending Composition