This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 6 years ago.
Can someone tell me why I'm getting the error Cannot find symbol when I'm creating an object? in bank_account Christine = new bank_account();
public class BankAccountTester
{
public static void main (String []args)
{
bank_account Christine = new bank_account();
Christine.deposit(10000);
Christine.withdraw(2000);
System.out.println(Christine.getBalance());
}
}
this is my class
public class bank_account{
private double balance;
public bank_account()
{
balance = 0;
}
public bank_account (double initialBalance)
{
balance = initialBalance;
}
public void deposit(double amount)
{
balance = balance + amount;
}
public void withdraw (double amount)
{
balance = balance - amount;
}
public double getBalance()
{
return balance;
}
}
The program is fully functional, I've tried your code and it works fine.
This is your compact code: https://gist.github.com/anonymous/8f055d981b27de4ce75c8c64d9a58498
Just go on www.compilejava.net and 'compile and run' it there.
It works on compilejava.net and on my netbeans too.
I think there's a problem with your compiler. You can try to reset it or even clean the cache: Deleting this directory should clear the cache for you- C:\Users\username.netbeans\7.0\var\cache //adapt with your version
An other error could be in creating project. In netbeans you have to create a project for each app you're developing. Have you did it and connected the 2 file of the program?
Related
i a beginner at java and I'm trying to compile 2 java codes Account.java and AccounTest.java and the command prompt keeps giving this message
"Main method not found in class Account, please define main method or a javaFX application class must extend javafx.application.Application"
researched on javafx apication and none was specific on what and where i should import. here is Account.java
"public class Account
private double balance;
public Account( double initialBalance )
{
if (initialBalance > 0.0)
balance = initialBalance;
}
public void credit( double amount)
{
balance = balance + amount;
}
public double getBalance()
{
return balance;
}
}
thanks in advance
So, I followed the online youtube tutorial for this, and it worked for the tutorial instructor, but not me... However, only because it wants a "getBalance" class in the source classes. If someone could help me with this, it would be greatly appreciated. I'm new to Java, and I do have some reading to catch up on in the book... So here's the program and its class setup:
BankAccountDemo.java
package bankaccountdemo;
import java.text.DecimalFormat;
public class BankAccountDemo {
public static void main(String[] args) {
BankAccount account1 = new BankAccount(12.00);
BankAccount account2 = new BankAccount(account1);
DecimalFormat dollar = new DecimalFormat("#.##0.00");
System.out.print("The balance in account #1 is $" + dollar.format(account1.getBalance()));
System.out.print("The balance in account #2 is $" + dollar.format(account2.getBalance()));
}
}
BankAccount.java
package bankaccountdemo;
public class BankAccount {
private double balance;
public BankAccount() {
balance = 0.0;
}
public BankAccount(BankAccount obj) {
balance = obj.balance;
}
public BankAccount(double startBalance) {
balance = startBalance;
}
}
I am aware that this is a pretty simple fix, but as I said.. I have some reading to catch up on. I understood the lottery problem better than this very simple bug.
it wants a "getBalance" class in the source classes.
It doesn't want a class it wants a method that is called on your bankaccount object.
account1.getBalance()
So you need to create a method in your BankAccount class.
public double getBalance(){
return balance;
}
This function is called a getter function. In OOP languages an object's properties are usually created as private and can be modified/set and read/get using this setter and getter functions.
so you can create another function like
public void setBalance(double balance){
this.balance = balance;
}
and then you can use
account1.setBalance(10.0);
to set the amount to 10.0
As you said, you need a getBalance() method in BankAccount:
public double getBalance() {
return balance;
}
This question already has answers here:
What causes "'void' type not allowed here" error
(7 answers)
Closed 10 months ago.
I'm learning to use classes and part of my assignment is to make this Car class. I'm getting an error on line 6 where I attempt to print of the results of the methods within the class. I think this means that I'm attempting to print something that doesn't exist and I suspect it's the mileage method. I tried changing it to return miles, but that didn't work either. Any ideas?
public class TestCar {
public static final void main(String args[]) {
Car c = new Car ();
c.moveForward(4);
System.out.println ("The car went" + c.mileage() + "miles."); // <-- L6
}
}
class Car {
public int miles = 2000;
public void moveForward(int mf) {
if (miles != 2000) {
miles += mf;
}
}
public void mileage() {
System.out.print(miles);
}
}
The error message is telling you exactly what is wrong -- you're trying to extract a result from a method that does not return a result.
Instead, have the mileage() method return a String, not print out a String.
public String mileage() {
return String.valueOf(miles);
}
Myself, I'd make this a getter method, and instead would do:
public int getMiles() {
return miles;
}
Car.mileage() is void, i.e., does not return anything. It needs to return something, like in:
public int mileage() {
return miles;
}
This question already has answers here:
"Error: Main method not found in class MyClass, please define the main method as..."
(10 answers)
Closed 9 years ago.
Sorry guys I am new to Java and I have an issue with my code. I have read through the threads and have seen many examples regarding this specific error (java.lang.NoSuchMethodError: main Exception in thread "main"). I just cant seem to wrap my head around where I would add (static void main(String[] args)) to the code. If you guys can point me in the right direction I would really appreciate it.
Here is what I have:
public class Employee {
String name;
String department;
double hourlyRate;
Employee(String name, String department, double hourlyRate) {
this.name = name;
this.department = department;
this.hourlyRate = hourlyRate;
}
public void setDepartment(String department) {
this.department = department;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public String getNameAndDepartment() {
return name + " " + department;
}
double weeklyPay(int numOfHourWorked) {
if (numOfHourWorked < 40) {
return (numOfHourWorked * hourlyRate);
} else
return (40 * hourlyRate);
}
}
class UnionEmployee extends Employee {
double dues;
UnionEmployee(String name, String department, double hourlyRate, double dues) {
super(name, department, hourlyRate);
this.dues = dues;
}
public void setDues(double dues) {
this.dues = dues;
}
double weeklyPay(int numOfHourWorked) {
if (numOfHourWorked <= 40) {
return (super.weeklyPay(numOfHourWorked));
} else
return ((super.weeklyPay(40) + ((numOfHourWorked - 40) * hourlyRate * 1.5)) - dues);
}
}
class CommissionEmployee extends Employee {
double commisionRate;
double salesAmount;
CommissionEmployee(String name, String department, double hourlyRate) {
super(name, department, hourlyRate);
}
public void setCommisionRate(double commisionRate) {
this.commisionRate = commisionRate;
}
public void setSalesAmount(double salesAmount) {
this.salesAmount = salesAmount;
}
double weeklyPay(int numOfHourWorked) {
return (super.weeklyPay(numOfHourWorked) + (commisionRate * salesAmount));
}
}
class TestEmployee {
UnionEmployee uEmp = new UnionEmployee(null, null, 0, 0);
CommissionEmployee cEmp = new CommissionEmployee(null, null, 0);
Employee emp = new Employee(null, null, 0);
void display(Employee emp, int numOfHourWorked) {
System.out.println("Name and department :" + emp.getNameAndDepartment ());
System.out.println("Weekly pay of employee :"
+ emp.weeklyPay(numOfHourWorked));
}
void display(UnionEmployee uEmp, CommissionEmployee cEmp,
int numOfHourWorked) {
System.out.println("Weekly Pay for UnionEmployee"
+ uEmp.weeklyPay(numOfHourWorked));
System.out.println("Weekly Pay for UnionEmployee"
+ cEmp.weeklyPay(numOfHourWorked));
}
}
OK so I started by separating each class into a different file. In looking through the Java tutorials it said to add static void main(String[] args) the way the tutorial had it setup was like this:
public class Misc {
static void main(String[] args) {
//body
}
}
So I did this:
class TestEmployee {
static void main(String[] args) {
UnionEmployee uEmp = new UnionEmployee(null, null, 0, 0);
CommissionEmployee cEmp = new CommissionEmployee(null, null, 0);
Employee emp = new Employee(null, null, 0);
void display(Employee emp, int numOfHourWorked) {
System.out.println("Name and department :" + emp.getNameAndDepartment ());
System.out.println("Weekly pay of employee :"
+ emp.weeklyPay(numOfHourWorked));
}
void display(UnionEmployee uEmp, CommissionEmployee cEmp,
int numOfHourWorked) {
System.out.println("Weekly Pay for UnionEmployee"
+ uEmp.weeklyPay(numOfHourWorked));
System.out.println("Weekly Pay for UnionEmployee"
+ cEmp.weeklyPay(numOfHourWorked));
}
}
}
Still get the same error : (java.lang.NoSuchMethodError: main Exception in thread "main").
OK I add public but now I get this:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
void is an invalid type for the variable display
Syntax error on token "(", ; expected
Duplicate local variable emp
Syntax error on token ",", ; expected
Syntax error on token ")", ; expected
void is an invalid type for the variable display
Syntax error on token "(", ; expected
Duplicate local variable uEmp
Syntax error on token ",", ; expected
Duplicate local variable cEmp
Syntax error on token ",", ; expected
Duplicate local variable numOfHourWorked
Syntax error on token ")", ; expected
at TestEmployee.main(TestEmployee.java:9)
Your problem with main is that it doesn't exist, and you need to put one in your program for it to run. Put it in the main class, whichever one that is, but while it needs to be inside of the class, inside of the curly braces that define the class, you must also make sure that you don't put it inside of another method.
Above, I'd put it in TestEmployee.
I'd also take care to make sure every class above is declared public and is in its own file. So your code above which contains 4 classes, should be comprised of 4 files.
Edit
Also, be sure to declare your main method as a public method as #Aniket noted in comment below.
Edit 2
You're still not declaring main as a public method.
You have methods embedded within the main method. Remember that in Java you can't do this since all methods need to be class level. Get them out of the main method.
Your code indentation is horrible to say the least, and this will make it very difficult for you or us to see your coding problems. You will want to invest time and effort towards indenting your code properly. If you did this, you would see in an instant that you had methods inside of methods.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm having trouble with making a Tester class for my original code.
So far I have
public class BankAccount
{
public BankAccount(double b, String John)
{
double balance = b;
String name = John;
}
public double deposit(double balance)
{
double d = (505.22 + balance);
return d;
}
public double withdraw(double balance)
{
double w = (balance - 100);
return w;
}
public double balance;
public String name;
}
Now what I need to do is make a tester class which incorporates the deposit and withdraw method. I'm having trouble coding the Tester class part.
What I have so far:
public class Tester1
{
public static void main(String args[])
{
BankAccount myAccount = new BankAccount(1000.00, "John");
System.out.println( myAccount.deposit(505.22) ); // my error is here.
}
}
the error is "method deposit in class BankAccount cannot be applied to given types;
rquired: no arguments; found double; reason: actual and formal arguements lists differ in length.
How It compiles ? Your class name is BankAccountTest
public class BankAccountTest{
You are creating object for BankAccount
BankAccount myAccount = new BankAccount(1000.00, "John");
So that line should be
BankAccountTest myAccount = new BankAccountTest(1000.00, "John");
Note: Shift to better IDE, if you are not using otherwise you end up with solving these type of compile time issues.
Your issue is that you are trying to return a value with depoisit which is a setter method. These do not return values
public double deposit(double balance)
{
double d = (505.22 + balance);
return d;
}
Change this to:
public void deposit(double balance)
{
double d = (505.22 + balance);
}
Then System.out.println the getter that DOES return a value.