Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-27-2012, 02:48 AM   PM User | #1
dannyboi
New Coder

 
Join Date: Jul 2012
Location: NYC
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
dannyboi is an unknown quantity at this point
Problems with the code

Here is my code ( or rather, the code that I ran from oracle)
public class Main{
class Bicycle {
}
int cadence = 0;
int speed = 0;
int gear = 1;

void changeCadence(int newValue) {
cadence = newValue;
}

void changeGear(int newValue) {
gear = newValue;
}

void speedUp(int increment) {
speed = speed + increment;
}

void applyBrakes(int decrement) {
speed = speed - decrement;
}

void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear);
}



class BicycleDemo {
public static void main(String[] args)
{
// Create two different
// Bicycle objects
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
}
{
// Invoke methods on
// those objects
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
}
{
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}



Now it wouldn't run through, and I assumed that it was the curly braces that didn't match up. I checked, but the code still would not run. Does anybody have any suggestion?
dannyboi is offline   Reply With Quote
Old 07-27-2012, 03:40 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by dannyboi View Post
class BicycleDemo {
public static void main(String[] args)
{
// Create two different
// Bicycle objects
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
}
{
// Invoke methods on
// those objects
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
}
What are you doing with the curly braces there? Whilst you can open and close braces randomly in Java, the main does have a closing brace which places the calls on bike1 and bike2 later within the class body which isn't applicable.

Are these both supposed to be combined in the class main? If so, it is missing a closing brace somewhere, else it's still missing a closing brace but I don't know where it would go. Nested classes also cannot have a main method.

I'm also trying to figure out if the properties and the methods actually belong to the Bicycle (which they appear to) or if they belong to the main. The current code has them in main.
PHP Code:
public class Main{
    class 
Bicycle {
    } 
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
java, java programming

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:45 AM.


Advertisement
Log in to turn off these ads.