Reputation:

Java methods and classes, how do they fit together?

Currently I am writing a program for an introductory Java class. I have two pieces to my puzzle. Hopefully this is a relatively simple to answer question.

Firstly, here is what I am trying to use as my main program:

import java.util.Scanner;

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


    Scanner input = new Scanner(System.in);


    double newBalance = 0;
    double monthlyInterest = 0;
    int answer = 0;


    int i=1;
    while (i < 100) {
        System.out.print ("Please enter your ID: ");
        answer = input.nextInt();
        System.out.println(" ");
        if (answer >=0 && answer<10) 
        TheATMGame.runGame (answer);
        else
         System.out.println("Sorry, this ID is invalid.");
     }
    }


    public static void runGame(int id) {
      double amount = 0;
      int continueOn = 0;
      while (continueOn < 4) {
        ATMGame myATM = new ATMGame();
        Scanner input = new Scanner(System.in);
        System.out.println ("---Main menu--- ");
        System.out.println ("1: Check balance ");
        System.out.println ("2: Withdraw ");
        System.out.println ("3: Deposit ");
        System.out.println ("4: exit ");

        int answer = input.nextInt();

        if (answer == 1)
            System.out.println("your balance is: " + myATM.getBalance (id));
        else if (answer == 2){
            System.out.println("Enter an amount to withdraw: ");
            amount = input.nextInt();
            myATM.withdraw(amount, id);
        }
        else if (answer == 3)
{
            System.out.println("Enter an amount to deposit: ");
            amount = input.nextInt();
            myATM.deposit(amount, id);
        }
        else if (answer == 4)
            continueOn = 4;
        else if (answer > 4)
            System.out.println ("Please review the main menu. " +
                    "Your selection must be between 1-4.");
      }
    }

//ATM class (balance, annualInterestRate2, id2)
//ATM myATM = new ATM (20000, 4.5, 1122 );
//newBalance = myATM.withdraw(2500);
//newBalance = myATM.deposit(3000);
//monthlyInterest = myATM.getMonthlyInterestRate();
//System.out.println("Your current balance is: " + newBalance);
//System.out.println ("Your monthly interest rate is: " +  monthlyInterest);



}

Now here are all of the classes I want to impliment into that program:

import java.util.Date;

public class ATMGame  {

    private double annualInterestRate = 0;
    private double balance = 0;
    private int id = 11;
    private int[] ids = {0,1,2,3,4,5,6,7,8,9};
    private int[] balances = {100,100,100,100,100,100,100,100,100,100};
    public Date dateCreated;


    public ATMGame() {

    }
    public ATMGame (double balance2, double annualInterestRate2, int id2) {
        balance = balance2;
        annualInterestRate = annualInterestRate2;
        id = id2;
        dateCreated.getTime();
    }


    public double getMonthlyInterestRate() {
        double monthlyInterest = annualInterestRate/12;
        return monthlyInterest;
    }


    public double withdraw(double amountWithdrawn, int id) { //This method withdraws money from the account

        double newBalance = balances[id] - amountWithdrawn;
        System.out.println("Your withdrawel has processed. New balance: " + newBalance);
        balances[id] = (int) newBalance;
        return newBalance ;

    }


    public double deposit(double amountDeposited, int id) { //This method deposits money in the account
        double newBalance = balances[id] + amountDeposited;
        System.out.println("Your deposit has processed. New Balance is: " + newBalance);
        balances[id] = (int) newBalance;
        return newBalance ;

    }

    public double getBalance(int id) {
        double myBalance = balances[id];
        balance = myBalance;
        return myBalance ;
    }


}

When I try to run the first program it says "No Main classes found." As you can see I have written the line " public void Main() ..." to take care of this, but eveidently it does not work. What am I doing wrong?

Replacing "public void Main() {" with "public static void main(String[] args) {" still returns the error: "No Main classes found." :/

http://img21.imageshack.us/img21/9016/asdfsdfasdfg.jpg

I was able to fix it by changing Main.java to TheATMGame.java and then running from ATMGame.java.

Upvotes: 1

Views: 4364

Answers (5)

RobbieV
RobbieV

Reputation: 444

The names of your public classes should match the file names. This is not the case in your screenshot. Also make sure that everything compiles correctly and then retry (using a public static void main(String[] args) { ... } method).

Upvotes: 0

Cuga
Cuga

Reputation: 17904

In your class ATMGame, replace the following:

public void Main() {

with:

public static void main(String[] args) {

Additionally, since this method has to be static, you'll need to change the following:

if (answer >=0 && answer<10)
     runGame (answer);
 else

with:

if (answer >=0 && answer<10)
     ATMGame.runGame (answer);
 else

Then finally, you need to change the method signature of rungame to also be static. Change it from:

public void runGame(int id) {

to:

public static void runGame(int id) {

Upvotes: 1

Steven Devijver
Steven Devijver

Reputation: 2563

Your method signature must be:

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

That's the convention you have to follow. Anything else won't work.

Upvotes: 3

Dan Lew
Dan Lew

Reputation: 87420

You've just misdefined main. Should be:

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

You're going to run into some other problems with your code, though. From a quick glance...

  • Your main() is a function, as well as runGame() - one shouldn't be defined within the other.
  • You cannot name your two classes the same thing - call the main() class something different than ATMGame.
  • Not sure where you're going with ATM class (balance, annualInterestRate2, id2), but it's not valid Java.

Upvotes: 3

Fabio Vinicius Binder
Fabio Vinicius Binder

Reputation: 13214

You should use:

public static void main(String[] args)

Instead of Main because the JVM calls this method first. It is a convention.

Upvotes: 3

Related Questions