Re: constructor problem
From: leslie chard (lost_at_one.net.au.invalid)
Date: 08/12/04
- Next message: Gordon Beaton: "Re: Problem excuteing RPM form java"
- Previous message: Peter Palsherm: "Problem excuteing RPM form java"
- In reply to: Chris Smith: "Re: constructor problem"
- Next in thread: Chris Smith: "Re: constructor problem"
- Reply: Chris Smith: "Re: constructor problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 12 Aug 2004 09:19:21 GMT
>
> Unfortunately, the code that you posted is incomplete, and doesn't
> compile because it's riddled with syntax errors. Since you talked about
> being able to compile and test the code, I'll assume that's not your
> real code.
its part of the real code if there are syntax errors in it might explain
why i am leaning so slow :-)
i compile it and get no errors and the group talks about not posting lots of
code so i thought i would only post the relevant bit
here is the full code for both tester and class
some of the variables have been declared but not used yet
hope this helps
both compile and run ok but the static method or variable totalOwing isn't
doesn't seem to be working correctly
we have been given a heap of these sort of exercises to do to help get our
heads round objects and classes ,instance variables, the static methods seem
to have thrown me abit i think
/*This is the tester class for testing the specialAccount2 object
* lelsie chard 9/8/04 version 1 student nuber 9051457394
*/
class SpecialAccountTest2 {
public static void main(String[] args){
// make a black constuctor and set attributes to defaults
SpecialAccount2 b = new SpecialAccount2();
b.setCustomerName("J Smith") ;
b.setAmountOwing(125) ;
b.setStatus(2) ;
b.setDiscount(0.10) ;
b.showAccount();
// 2nd constructor
SpecialAccount2 Emma = new SpecialAccount2();
Emma.setCustomerName("Emma ") ;
Emma.setAmountOwing(0.0) ;
Emma.setStatus(1) ;
Emma.setDiscount(0.20) ;
Emma.showAccount();
SpecialAccount2 Rodney = new SpecialAccount2("Rodney",555);
//Rodney.setCustomerName() ;
//Rodney.setAmountOwing() ;
Rodney.setStatus(2) ;
Rodney.setDiscount(0.10) ;
Rodney.showAccount();
SpecialAccount2 Leslie = new SpecialAccount2("Leslie ",1000,2);
//Leslie.setCustomerName(" ") ;
//Leslie.setAmountOwing(0) ;
//Leslie.setStatus(0) ;
Leslie.setDiscount(0.10) ;
Leslie.showAccount();
}//end main
}//end class
//--we are going to build a a second special account class
// 9/8/04, leslie chard,student nuber 9051457394 Version1
//--import statements
import java.text.*;
import java.util.*;
public class SpecialAccount2{
//--instance variables
private double discount ;
private double discountLvl1 =0.10 ;
private double amountOwing ;
private String customerName ;
private String dateIssued ;
private int status ;
Date today = new Date() ;
SimpleDateFormat dateYear = new SimpleDateFormat("dd-MMM-yy");
String dateY = (dateYear.format(today));
DecimalFormat dfmt = new DecimalFormat("$0.00") ;
DecimalFormat afmt = new DecimalFormat("0%") ;
// couple of constants
private final int gold = 1 ;
private final int silver = 2 ;
// static properties/field
private static int accountCount = 0 ;
private static double totalOwing = 0 ;
//1st constructor
//The 'no-args' constructor. Used to create a 'default' or blank object
public SpecialAccount2(){
amountOwing = 0.0 ;
customerName ="" ;
dateIssued = dateY ;
status = 0 ;
discountLvl1 = 1 ;
accountCount++ ;
totalOwing += amountOwing ;
} //end 1st constructor
//2nd constructor
public SpecialAccount2(String namex){
amountOwing = 23.45 ;
customerName = namex ;
dateIssued = dateY ;
status = 0 ;
if(status == gold) {
discount = 0.20 ;
}else {
status = silver ;
discount = 0.10 ;
}
accountCount++ ;
totalOwing += amountOwing ;
}//end 2nd constructor
//3rd constructor
public SpecialAccount2(String namex,double newAmountOwing){
amountOwing = newAmountOwing ;
customerName = namex ;
dateIssued = dateY ;
status = 0 ;
if(status == gold){
discount = 0.20 ;
}else {
status = silver ;
discount = 0.20 ;
}
accountCount++ ;
totalOwing = totalOwing + amountOwing ;
}//end 3rd constructor
//4th constructor
public SpecialAccount2(String namex,double newAmountOwing,int Status ){
customerName = namex ;
amountOwing = newAmountOwing ;
status = Status ;
dateIssued = dateY ;
if(status == gold) {
discount = 0.20 ;
}else {
Status = silver ;
discount = 0.10 ;
}
accountCount++ ;
totalOwing += amountOwing ;
}//end 4th constructor
//--accessors
//an 'Accessor' for the customer name attribute. Makes the name available
to external objects
public String getCustomerName(){
return customerName;
}
public double getAmountOwing(){
return amountOwing ;
}
public int getStatus(){
return status ;
}
public double getDiscount(){
return discount ;
}
public double getDiscountLvl1(){
return discountLvl1 ;
}
//--mutators
// a 'Mutator' for the customer name attribute. Allows external objects
to changed the name attribute of this object,
// replacing it with a new value.
public void setCustomerName(String newName){
customerName = newName;
}
public void setAmountOwing(double newAmountOwing){
amountOwing = newAmountOwing;
}
public void setStatus(int newStatus){
status = newStatus ;
}
public void setDiscount(double newDiscount){
discount = newDiscount ;
}
public void setDiscountLvl1(int newDiscountLvl1){
discountLvl1 = newDiscountLvl1 ;
}
//------ Static methods -----------
public static int getAccountCount(){
return accountCount;
}
public static double getTotalOwing(){
return totalOwing ;
}
//--other methods
public void showAccount(){
System.out.println(" Date Issued : " + dateYear.format(today) + "\n
Customer : " + customerName + "\n status : " + status ) ;
System.out.println(" Discount : " + afmt.format(discount)) ;
System.out.println(" Amount oweing : " + dfmt.format(amountOwing)) ;
System.out.println(" There are " + accountCount + " accounts in the
system") ;
System.out.println(" The total amount owing is " +
dfmt.format(totalOwing)) ;
System.out.println("********************") ;
}
//-- main method
}//end class
- Next message: Gordon Beaton: "Re: Problem excuteing RPM form java"
- Previous message: Peter Palsherm: "Problem excuteing RPM form java"
- In reply to: Chris Smith: "Re: constructor problem"
- Next in thread: Chris Smith: "Re: constructor problem"
- Reply: Chris Smith: "Re: constructor problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|