PDA

View Full Version : Does Anyone Speak Java: The Sequel


Hotwheelz
02-19-2011, 06:24 PM
I'm having trouble debugging this:

/*
Chapter 3: Free Time Calculator
Programmer: Censored
Date: February 19, 2011
Filename: FreeTimeCalc.java
Purpose: This project calculates your free time based
on time spent sleeping, at school/work and driving.
*/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class FreeTimeCalc extends Applet implements ActionListener
{
//declare variables
Image logo; //declare an image object
int sleepTime, workTime, driveTime;
double hoursInAWeek = 168.0;
double freeTime;

//construct components
Label swcLabel = new Label("CENSORED FREE TIME CALCULATOR");
Label sleepLabel = new Label("Enter how much time you sleep per week to the nearest hour: ");
TextField sleepField = new TextField(10);
Label workLabel = new Label("Enter how much time you work per week to the nearest hour: ");
TextField workField = new TextField(10);
Label driveLabel = new Label("Enter how much time you drive per week to the nearest hour: ");
TextField driveField = new TextField(10);
Button calcButton = new Button("Calculate");
Label outputLabel = new Label(
"Click the calculate button to see your free time.");

public void init()
[
setForeground(Color,red);
add(swcLabel);
add(sleepLabel);
add(sleepField);
add(workLabel);
add(workField);
add(calcButton);
add(driveLabel);
add(driveField);
calcButton.addActionListener(this);
add(outputLabel);
logo = getImage(getDocumentBase(), "logo.gif");
}

public void actionPerformed(ActionEvent e)
{
sleepTime = Integer.parseInt(sleepField.getText());
workTime = Integer.parseInt(workField.getText());
driveTime = Integer.parseInt(driveField.getText());
double freeTime = 1 - (driveTime + sleepTime + workTime) / hoursInAWeek;
outputLabel.setText("You are free " + (Math.round(freeTime) * 100) + "% of the time.");
}

public void paint(Graphics g)
{
g.drawImage(logo,125,160,this);
}
}Here's the errors:

F:\School\CIS 153\Lab 5\FreeTimeCalc.java:34: <identifier> expected
public class void init()
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:34: invalid method declaration; return type required
public class void init()
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:34: ';' expected
public class void init()
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:37: invalid method declaration; return type required
add(swcLabel);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:37: <identifier> expected
add(swcLabel);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:38: invalid method declaration; return type required
add(sleepLabel);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:38: <identifier> expected
add(sleepLabel);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:39: invalid method declaration; return type required
add(sleepField);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:39: <identifier> expected
add(sleepField);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:40: invalid method declaration; return type required
add(workLabel);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:40: <identifier> expected
add(workLabel);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:41: invalid method declaration; return type required
add(workField);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:41: <identifier> expected
add(workField);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:42: invalid method declaration; return type required
add(calcButton);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:42: <identifier> expected
add(calcButton);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:43: invalid method declaration; return type required
add(driveLabel);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:43: <identifier> expected
add(driveLabel);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:44: invalid method declaration; return type required
add(driveField);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:44: <identifier> expected
add(driveField);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:45: <identifier> expected
calcButton.addActionListener(this);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:45: illegal start of type
calcButton.addActionListener(this);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:46: invalid method declaration; return type required
add(outputLabel);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:46: <identifier> expected
add(outputLabel);
^
F:\School\CIS 153\Lab 5\FreeTimeCalc.java:47: <identifier> expected
logo = getImage(getDocumentBase(), "logo.gif");
^
24 errors

Tool completed with exit code 1
The culprit seems to be public void init().</identifier></identifier></identifier></identifier></identifier></identifier></identifier></identifier></identifier></identifier></identifier></identifier>

Ray_Lewis'_Victim
02-19-2011, 06:29 PM
Looks like you're opening void init() with a square bracket, not a curly brace.

CEH
02-19-2011, 06:32 PM
public void init()
[


You have a left bracket instead of a left brace "{"

Hotwheelz
02-19-2011, 06:33 PM
Looks like you're opening void init() with a square bracket, not a curly brace.
I feel smart. Thanks.

Play2win
02-19-2011, 07:28 PM
I'm in the middle of Cramming for my JAVA class. Textbook is the Comprehensive intro to Java by Y. Daniel Liang. Real good book. But ONLINE courses suck, especially online 8 week courses... in programming... I'll find out in a couple of weeks how this goes YAAAA!!!! :holyguac!

ghwk
02-19-2011, 08:44 PM
I took fortran once so I am of no use to you. I am also of no use to you because I had to drop the class when I couldn't make a random number generator.

Mr. Elway
02-20-2011, 08:24 AM
Quit posting your homework on the forum and figure it out yourself. People are going to be relying on you to program on your own one day!

theAPAOps5
02-20-2011, 08:33 AM
Quit posting your homework on the forum and figure it out yourself. People are going to be relying on you to program on your own one day!

Yeah asking for help is unamercian. Don't ever ask for help. Hilarious!

HILife
02-20-2011, 08:57 AM
<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/KywvdyDlhKg" frameborder="0" allowfullscreen></iframe>