Please help a newbie with a easy java assignment!!
Hi, I'm new with the java world..
I have a homework assignment for my java class due soon and I'm quite stuck in the middle of it.
I'm supposed to make an eight ball using GOval in the acm library and whenever I click on the black ball in the applet, the words (fortunes) need to be changed everytime. But another click after showing the fortune, it must come back and show me 8 again and another click will lead to another fortune and another will come back to 8.
import acm.program.GraphicsProgram;
import acm.graphics.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import acm.util.RandomGenerator;
public class EightBall extends GraphicsProgram
{
public static final int BALL_RADIUS = 100;
public static GObject obj;
private GLabel fortunes = new GLabel("");
public String fortune[];
RandomGenerator rgen = new RandomGenerator();
public void run()
{
addMouseListeners();
createBall();
getFortune();
}
public void mouseClicked(MouseEvent e)
{
boolean startAtEight = false;
if(startAtEight)
{
}
// obj = getElementAt(getX(), getY());
//
// while(obj != null)
// {
// println(fortunes);
// }
}
public void createBall()
{
GOval ball = new GOval(BALL_RADIUS*2,BALL_RADIUS*2);
ball.setColor(Color.BLACK);
ball.setFilled(true);
add(ball);
}
public void getFortune()
{
fortunes.setFont(new Font("Ariel", Font.BOLD, 15));
fortunes.setColor(Color.WHITE);
String [] fortune = new String[7];
fortune[0] = "8";
fortune[1] = "Yes";
fortune[2] = "No";
fortune[3] = "Most Likely";
fortune[4] = "Maybe";
fortune[5] = "Make it work";
fortune[6] = "Not going to happen";
fortunes.setLabel(fortune[rgen.nextInt(7)]);
fortunes.setLocation((BALL_RADIUS*2-fortunes.getWidth())/2, BALL_RADIUS);
add(fortunes);
}
}
This is what I got so far and I'm stuck in the middle of it..
This is the requirements:
Pick from a set of 6 fortunes choosing one randomly
Methods
In addition to the run method, you will make at least 3 other methods:
- One to create the eight ball
- One to pick a random fortune
- And a mouseClicked method that will change the fortune
The part that I'm stuck at right now is how to make the applet start at 8 in the beginning and it have to show fortune after a click then if i click another it will be back to 8, then another a fortune and so on.
If you could help me get this through, I would be very appreciate it because I have no idea how to get going from here.. I will be waiting for any reply from now on.
Thank you so much
Re: Please help a newbie with a easy java assignment!!
Quote:
Originally Posted by
schoolbus11
...I will be waiting for any reply...
Maybe something like this:
Create a boolean instance variable named, say Showing8.
Code :
Applet init() function:
BEGIN
Perform other necessary initialization tasks. (Add mouse listener, etc.)
Set Showing8 to true.
Select the 8-ball label.
Do whatever you have to do to draw/paint the screen.
END init() function
And
Code :
Mouse click event handler:
BEGIN
IF Showing8 IS true
THEN
Select a random fortune label.
SET Showing8 to false. // For next click
ELSE
Select the 8-ball label.
SET Showing8 to true. // For next click
END IF
Do whatever you have to do to draw/paint the screen
END of Mouse click event handler.
Cheers!
Z
Re: Please help a newbie with a easy java assignment!!
if this is easy than i m screwed
Re: Please help a newbie with a easy java assignment!!
The stuff in my post is called pseudo-code. Have you never been exposed to pseudo-code?
The things that I posted are not supposed to be Java, but they are supposed to suggest (in algorithmically-oriented Human language, not a Programming language) a way to approach the part of the task that you specifically asked about.
Namely
Quote:
Originally Posted by kloek8
The part that I'm stuck at right now is how to make the applet start at 8 in the beginning and it have to show fortune after a click then if i click another it will be back to 8, then another a fortune and so on.
There are certainly other approaches, and maybe some of them might appear simpler to some folks, but that's just as about as simple as I can make it (coming off the tops of my heads in my usual stream-of-consciousness mode of expression).
On the other hand...
If you are new to Java and your very first assignment (in a class that has no prerequisites involving basic Java knowledge), is an applet graphics program using acm library functions, then maybe you are, indeed, screwed.
After all, I'm not the one who called it "easy." I mean, as my old pal Junior Samples used to say, "If it wuz easy, anybody could do it!"
Cheers!
Z
Re: Please help a newbie with a easy java assignment!!
Quote:
Originally Posted by
kloek8
if this is easy than i m screwed
Love your sense of humor and that comment made my day. ty
Re: Please help a newbie with a easy java assignment!!
it is solved now :) thanks!
Re: Please help a newbie with a easy java assignment!!
Please surround your code with [ highlight=java] and [ /highlight] tags. (without the space following each leading bracket)
Going by the desired operation in your first post, I would say to drop the while loop from your mouseClicked method completely. Then using your boolean in your if, test to see if you should be showing your 8 or your message, and respond accordingly in either the if block or the else block.
Code JAVA:
myFunkyMethodForClicks(ignoredClickSpecs ics) {
if(messageIsShowing) {
display 8;
}
else {
display random message;
}
toggleBoolean;
}//end myFunkyMethod