Java methods and classes, how are they combined?
I am currently writing a program for an introductory Java class. I have two pieces of my puzzle. Hopefully this is a relatively simple answer.
First, this 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 the classes that I want to implement in this 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". As you can see, I wrote the line "public void Main () ..." to take care of this, but it doesn't seem to 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 managed to fix this by changing Main.java to TheATMGame.java and then running from ATMGame.java.
In your class, ATMGame
replace the following:
public void Main() {
from:
public static void main(String[] args) {
Also, since this method must be static, you need to change the following:
if (answer >=0 && answer<10)
runGame (answer);
else
from:
if (answer >=0 && answer<10)
ATMGame.runGame (answer);
else
Then finally you need to change the method signature rungame
, also static. Change it:
public void runGame(int id) {
at
public static void runGame(int id) {
source to share
You just misidentified the main one. Should be:
public static void main(String[] args) {
....
}
However, you will run into some other problems with your code. With a quick glance ...
- Your main function () is a function and also runGame () - shouldn't be defined elsewhere.
- You cannot name your two classes the same - call the main () class with something other than ATMGame.
- Not sure where you are going from
ATM class (balance, annualInterestRate2, id2)
, but this is not valid Java.
source to share