You are not logged in.
Pages: 1
Hello everyone, i have a homework that i've been trying to do for like 2 weeks, it's a program to simulate a client with several bank accounts, each account has an account number and his balance...I've done everything else and the last thing i need to do is create a static array that holds the accounts in it.
I already have a class "Compte" which is a class for one account, and the objective is to create another class "Client" which holds several accounts so i guess i should create objects from the "Compte" class and then store the created objects in the array, i created the array using :
Compte comptes[] = new Compte[10];
and then made the first object using:
Compte compte = new Compte(1);
and now the problem is storing "compte" object in the array, i try:
comptes[0] = compte;
and it gives me: cannot find symbol
symbol: class comptes
location: class client
']' expected
<identifier> expected
I'm using NetBeans IDE.
I asked the teacher several times why is it happening and everytime he says i should figure it out on my own and all he said to me is put the code in the constructor..(All the code above is in the Client class)
Question: why can't i store the object "compte" created earlier in the array, should i put the code elsewhere ?
Hello, it helps if you post more code, like the definition of the class, in which package, the class of where you are trying to define the Compte array and in which package and what are the imports in the top of the class.
are you wirting that code in class client? i agree anayman we need more code
do you have a constructor in class compte with 1 parameter?
Yea sorry i just wanted the post to be easy to read not a whole lot of text but guess it should be big
here is Compte class:
package compte;
/**
*
* @author A-pc
*/
public class Compte {
int numeroCompte;
double solde;
double tauxInterets = 1.05;
public Compte(int a){
numeroCompte = a*5;
solde = 0;
tauxInterets = 1.05;
}
public Compte(){
}
public void Depot(int d){
solde = solde + d;
}
public void Retrait(int r){
solde = solde - r;
}
public boolean Decouverte(){
if(solde<0)
return true;
else
return false;
}
public void AffectationInteret(){
solde = solde*tauxInterets;
}
public void Afficher(){
System.out.println("Numero de compte:"+numeroCompte);
System.out.println("Solde:"+solde);
}
public int Num(){
int a = numeroCompte;
return a;
}
public static void main(String[] args) {
Compte c = new Compte(1);
c.Depot(40000);
c.Retrait(6000);
c.AffectationInteret();
c.Afficher();
c.Decouverte();
}
}
and the Client class:
package compte;
/**
*
* @author A-pc
*/
public class Client {
Compte[] comptes = new Compte[5];
Compte c0 = new Compte(1);
Compte c1 = new Compte(2);
Compte c2 = new Compte(3);
Compte c3 = new Compte(4);
Compte c4 = new Compte(5);
comptes[0]=c0;
comptes[1]=c1;
comptes[2]=c2;
comptes[3]=c3;
comptes[4]=c4;
public void consommer (int num,int s){
for(int i = 0;i<5;i++){
if(comptes[i].numeroCompte == num){
comptes[i].Retrait(s);
}
}
}
public void ajouter (int num,int s){
for(int i = 0;i<5;i++){
if(comptes[i].numeroCompte == num){
comptes[i].Depot(s);
}
}
}
public void bilan(){
for(int i = 0;i<5;i++){
boolean check = comptes[i].Decouverte();
if (check == true){
System.out.println("Ce compte est decovert!");
}
}
}
public void balance(){
for(int i = 0;i<5;i++){
System.out.println("Numero du comptes:"+comptes[i].numeroCompte);
System.out.println("Solde:"+comptes[i].solde);
}
}
}
Sorry for late reply, connection was down..
Last edited by roudy5 (October 22 2016)
Post the whole exception so we can see where the problem exactly is.
The error is at
comptes[0]=c0;
comptes[1]=c1;
comptes[2]=c2;
comptes[3]=c3;
comptes[4]=c4;
and i posted earlier what error the IDE gives me.
(if that is what you are asking.. didnt quite understand what u meant)
You can't fill the array with values outside of a method.
You can't fill the array with values outside of a method.
100%, create a constructor for the client class,
public Client() {
comptes[0]=new Compte(1);
comptes[1]=new Compte(2);
comptes[2]=new Compte(3);
comptes[3]=new Compte(4);
comptes[4]=new Compte(5);
}
You don't need to create c1,c2,c3,c4 and c5 separately
Oh i see.. to make sure i understood it so when i have to fill the array it has to be inside of a constructor or function or main.. whatever the method is right ?
Thanks a lot guys really !
Make sure all methods name always start with a lowercase and continue as a camel notation. This will make it easier to identify the nature of the code constructs you are referring in java
Pages: 1