线程“main”中的异常java.lang.Error:未解决的编译问题(String)

时间:2014-03-23 14:52:13

标签: java

最终代码如下所示。修复了错误并理解了继承的概念。谢谢你们回复我的帖子。你的回复很有帮助。

帐户类

public class Account {
String name;
String number;
public double balance;


public class Account 
{
public String acctnum;
public String accttitle;
public Double acctbal;
public String accttitle2;
public Account()
{
}
public void withdraw(Double amount)
{
 acctbal=acctbal-amount;
}
public void deposit(Double amount)
{
  acctbal=acctbal+amount;    
}
public double zakat() /*In Islam Zakat is deducted every year from saving 
                        accounts in holy month of Ramadan  */
{
 Double z=acctbal*0.025;
 return z;
}
 public void showdetail()
{
 System.out.println("The Account Number  ="+acctnum);
 System.out.println("The Account Title   ="+accttitle);
 System.out.println("The Account Balance ="+acctbal);  
}
}

当前帐户类

public class CurrentAccount extends Account
{   
  public CurrentAccount(String Num,String Title,double Bal)
{
 acctnum = Num;
 accttitle = Title;
 acctbal = Bal;
}
public double tax()
{
double t= acctbal*0.05;
return t;        
}
}

联名账户类

public class JointAccount extends Account
{
public String accttitle2;
public JointAccount(String Num,String Title,String accttitle_1,double Bal)
{
  acctnum = Num;
  accttitle=Title;
  accttitle2=accttitle_1;
  acctbal  =Bal;
}
 public void showdetail()  //Overrding
{
System.out.println("The Account Number  = " + acctnum);
System.out.println("The Account Title   = " + accttitle + " and " + accttitle2);
System.out.println("The Account Balance = " + acctbal);
}
public double calculatetax()
{
 double t= acctbal*0.09;
 return t;
}
}

SavingAccount Class

public class savingAccount extends Account 
{
  public savingAccount(String Num,String Title,Double Bal)
{
  super.acctnum  =Num;
  super.accttitle=Title;
  super.acctbal  =Bal;
}
public double tax()
{
double t= acctbal*0.07;
return t;
}
public void withdraw(double amount)
{
   super.withdraw(amount);
     if(acctbal<1000){
     acctbal=acctbal-150;
 }
}

}

以下是testApp类

public class testApp 
{
public static void main(String args[])
{
savingAccount obj=new savingAccount("01-102-33","Ali",30000.0); 

System.out.println("My Balance is="+obj.acctbal);
obj.withdraw(5000.0);
System.out.println("After Withdraw The Balanc is="+obj.acctbal); 

obj.deposit(10000.0);
System.out.println("After deposit balance is  ="+obj.acctbal); 

double z= obj.zakat();
System.out.println("zakat is="+z); 

obj.withdraw(z);
System.out.println("after draw balance is="+obj.acctbal);

double t=obj.tax();
System.out.println("Tax is="+t);

obj.withdraw(t);
System.out.println("Remaining balance is ="+obj.acctbal);
obj.withdraw(25000.0);
obj.showdetail();

CurrentAccount obj1=new CurrentAccount("01-102-33","Ali",10000.0);
System.out.println("My Balance is="+obj1.acctbal);
t=obj1.tax();
System.out.println("Tax is ="+t);

obj1.withdraw(t);
System.out.println("Remaining bal is="+obj1.acctbal);

obj1.showdetail();

JointAccount obj2=  new JointAccount("11-101-23","Amjad","Usman",20000.0);

System.out.println("My Balance is="+obj2.acctbal);
t=obj2.calculatetax();
System.out.println("Tax is ="+t);
obj2.showdetail();
}
}

4 个答案:

答案 0 :(得分:1)

字符串应为大写S.将string更改为String。字符串类名称以'S'开头

String accountTitle;
String accountNumber;

答案 1 :(得分:1)

几点建议:

  • 帐户,目前的形式,不应该是Abstract(您没有定义任何只有签名(名称和参数)的方法,也没有实现方法的代码
  • 您希望每个类仅使用访问这些值的方法来保护其内部变量
  • 您希望使用正确的Java语法(查看变量类型,语法等)
  • 遵循Java约定(类名以大写字母开头;方法名称以小写字母开头,......)

考虑以下替换

<强> TestApp

public class TestApp {

public static void main(String[] args) {
    SavingAccount sa = new SavingAccount();

    sa.open("Amjad", "5440442378", 1000.00d);

    System.out.println("Account name: " + sa.getName() + ", number: " + sa.getNumber());

    System.out.println("Account balance: " + sa.getBalance());
    System.out.println("Account tax: " + sa.calculate(.05d));
}
}

帐户

public class Account {
String name;
String number;
public double balance;

public Account() {
};
public void open(String accountName, String accountNumber, double openingDeposit) {
    name = accountName;
    number = accountNumber;
    balance =  openingDeposit;
}

public void withdraw(double amount) {
    if (balance > amount) {
        balance -= amount;
    }
}
public void deposit(double amount) {
    balance = balance + amount;
}
public String getName() {return name;}
public String getNumber() {return number;}
public double getBalance() {return balance;}
}

SavingAccount (我怀疑你想要&#34;税收&#34;成为&#34;评价&#34;,并且您可能希望确定您的回报率&# 34;打开&#34; SavingAccount(即使用属于类的变量)

public class SavingAccount extends Account {

    public double calculate(double rate) {
        return balance * rate;

    }
}

答案 2 :(得分:0)

  1. string应为String
  2. 构造函数应为public Account()而不是public account()
  3. accoutTitle(String) is undefined for the type saving_Account:因为没有这样的方法,你需要定义它,还需要accountNumber(String)

    class saving_Account extends Account {
     public double calculate(double tax) {
        tax = balance * 0.05;
        return tax;
     }
    
     void accoutTitle(String accountTitle) {
      this.accountTitle=accountTitle;
     }
    
    void accountNumber(String accountNumber) {
      this.accountNumber=accountNumber;
     }
    }
    

答案 3 :(得分:0)

您的代码中存在许多问题:

  • 首先,Java区分大小写,因此string应为String,类似于您的构造函数。构造函数的名称应与您的类的名称完全匹配。

    String accountTitle;
    String accountNumber; //Notice string has been changed to String
    
  • 其次,您没有名为accoutTitle的方法,accountNumber