-4

I am still at HelloWorld when it comes to my lack of Java skills. I cannot understand this to save my life. I am working on writing an override method? And am just starting out with the code provided from my school before I even start trying to complete the rest and I am already getting errors that I cannot even begin to think of how to correct. Any help at all would be greatly appreciated. I just want this error to go away so I can create some new ones :) Here is the code:

public class Vehicle
public static void main (String [] args)}
{
private boolean moving; // whether or not the vehicle
private double speed; 
private char bearing; 
('N','E','S', or 'W')
public Vehicle(){ // Vehicle class no-arg constructor
    moving = false; // assume not moving
    speed = 0.0; // not moving
    bearing = 'N'; // assume 'N'orth
    System.out.println("Created a vehicle (no-arg)"); 
}
public Vehicle (double initialSpeed) // Vehicle 1-arg constructor
    bearing = 'W';
    speed = initialSpeed;
    if (speed > 0.0)
    {
        moving = true;
    }
    System.out.println("Created a vehicle (1-arg)"); 
public Vehicle (double initialSpeed, char initialBearing) // Vehicle 2-arg constructor
    bearing = initialBearing;
    speed = initialSpeed;
    if (speed > 0.0){
        moving = true;
    }
    System.out.println("Created a vehicle (2-arg)");
public void start(double initialSpeed, char initialBearing){
    moving = true;
    if (initialSpeed >= 5.0 && initialSpeed <= 20.0){
        speed = initialSpeed; // valid expected range
    } else if (initialSpeed >= 0.0 && initialSpeed < 5.0){
        speed = 5.0; // minimum
    } else if (initialSpeed < 0.0){
        speed = 0.0; // assume no movement
        moving = false;
    } else if (initialSpeed > 20.0){
        speed = 20.0; // maximum allowed
    }
    switch(initialBearing){
    case 'N':
        bearing = initialBearing;
        break;
    case 'E':
        bearing = initialBearing;
        break;
    case 'S':
        bearing = initialBearing;
        break;
    case 'W':
        bearing = initialBearing;
    default:
System.out.println("invalid bearing " + 
     initialBearing +
     " set to N"); // additional user notification
     bearing = 'N';
}
public double getSpeed() { // get and return current speed in mph
    return speed;
}
public void setSpeed(double newSpeed){ // set new speed in mph
     speed = newSpeed;
}
    /**
     *
     * @return
     */
public char getBearing(){
    return bearing;
}
public void speedUp(double mphSteps, int numSteps){
    int counter = 0;
    while (counter < numSteps)
    speed += mphSteps;
    System.out.println("counter= " + counter + ", " + 
    this.toString());
    counter++;
}
public String toString(){
    return "From toString(): speed= " + getSpeed() +
    " mph and bearing= " + getBearing();
} 
}
public class Car extends Vehicle{
    private String color;
    private int doors; 
    private double hp; 
    public Car(String carColor, int numDoors, 
    double horsePower, double
    startingSpeed) 
{
super(startingSpeed);
color = carColor;
doors = numDoors; 
hp = horsePower; 
System.out.println("Created a car"); 
}
public String getColor()
{
return color;
}
public int getDoors()
{
return doors;
}
public double getHp()
{
return hp;
}
public String toString()
{
return "From Car toString(): color= " + getColor() +
" doors= " + getDoors() + 
" hp= " + getHp() +
" speed= " + getSpeed() +
" mph and bearing= " + getBearing();} 
}
public class TestCar2
{
public static void main(String[] args)
{ 
Car myCar2 = new Car("blue", 4, 300., 10.0); 
System.out.println(myCar2.toString());
myCar2.speedUp(5.0, 2); 
}
}

Please Please and thank you for helping!

  • Is it under package vehicle? And what is this public static void main (String [] args)} ? – Juned Ahsan Aug 16 '13 at 06:29
  • Does that code even compile? – Rohit Jain Aug 16 '13 at 06:30
  • 2
    It would be great if you: 1) **Indent** the code. 2) Read the errors in your code (thrown by the compiler in messages). 3) If you don't understand the errors, post them and we will guide you. Note that this is not a *do the homework/exercise for me* site, but looks that you really want to learn, so please be a good student and provide us what we need to guide you. – Luiggi Mendoza Aug 16 '13 at 06:30
  • please display entire code with error your getting. – LMK Aug 16 '13 at 06:38
  • If you are just learning, the I suggest you find a simpler example to work from. – diestl Aug 16 '13 at 06:52
  • possible duplicate of [What does "Could not find or load main class" mean?](http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean) – Stephen C Aug 16 '13 at 10:46

2 Answers2

1
public class Vehicle

public static void main (String [] args)} // this is totally wrong
     // This not compile at least 
{

Actually parenthesis({}) you are using is totally wrong. Following structure should follow you. I am guessing that you are not using IDE to do coding. I suggest you to use IDE to do code.

public class MyClass{

 public static void main(String[] args){
   // main method
 }
 // some other method

}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Firstly You cannot have two public classes in a single file!!

Secondly you braces are completely unmatching.

public class Vehicle
public static void main (String [] args)}
{

Class has it's own scope and main() function has it's own. So change your code to

public class Vehicle {
public static void main (String [] args) { //your code}
}

your multiple arg constructors don't have proper brackets.

public Vehicle (double initialSpeed) // Vehicle 1-arg constructor
    bearing = 'W';
    speed = initialSpeed;
    if (speed > 0.0)
    {
        moving = true;
    }
    System.out.println("Created a vehicle (1-arg)"); 

change it to

public Vehicle (double initialSpeed) { // Vehicle 1-arg constructor
    bearing = 'W';
    speed = initialSpeed;
    if (speed > 0.0)
    {
        moving = true;
    }
    System.out.println("Created a vehicle (1-arg)"); 
}

lastly your switch statement you need to put break in all cases(except default) change

case 'W':
bearing = initialBearing;

to

case 'W':
bearing = initialBearing;
break;

If you are new to java and learning basics I suggest use an IDE like Eclipse, Netbeans or Intellij IDEA. Google them to find more info.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • I will change everything you both said! Thank you very much for your help. I am using Netbeans like you said! Thank you again. – Lacey Rushing Aug 16 '13 at 07:17
  • In the code I have public class Vehicles then I have public class Car extends Vehicles.. I am trying to add the ending of the code Public class TestCar 2 and I am getting the error main method not found. Know I am guessing that TestCar2 cannot extend from Car because why would I have even written Car if I am just going to extend from that, but my assignment needs me to modify TestCar2 and make a TestCar3. I have a Java book that might as well have been written in Mandarin. I just want to understand all of this. Is there a book or a website that anyone could reccomend for a newbie? – Lacey Rushing Aug 16 '13 at 08:50
  • run: Error: Main method not found in class vehicles.Vehicles, please define the main method as: public static void main(String[] args) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds) – Lacey Rushing Aug 16 '13 at 08:52
  • You must run the class that has the main() method. – Aniket Thakur Aug 16 '13 at 08:58