Originariamente inviato da Ironwill
codice:
public class test {
private String auth;
private String title;
private int time;
private double price;
void cd(String au, String tl, int tm, double pr){
this.auth = au;
this.title = tl;
this.time = tm;
this.price = pr;
}
}
class collaudo{
public static void main(String args[]){
test cd[] = new test[6];
cd[0]= cd ("Lady gaga", "The Fame monster", 118, 20.72);
cd[1]= cd ("Track 2 inediti", "Vasco Rossi", 95, 16.72);
cd[2]= cd ("if", "Mario biondi", 100, 17.52);
cd[3] = cd("Laura live tour","Laura pausini", 98, 19.50);
cd[4] = cd ("Crazy love", "Michael bublè", 90, 20.40);
cd[5] = cd("this is is", "Michael Jackson",120, 21.90);
for(int i=0;i<6; i++)
System.out.println(cd[1]);
la dichiarazione era identica ma il problema rimane, mi dice il metodo cd (string string, int, double) non è definito per il type collaudo e che ovviamente le variabili auth title ecc ecc non vengono usate. Dove diavolo sbaglio?
1. La tua classe si chiama test. Pessimo nome: stai rappresentando un CD, cosa c'entra quel nome? All'interno della classe test hai messo un metodo void cd che penso tu voglia usare come costruttore.. Solo che per essere usato come costruttore deve avere lo stesso nome della classe e non avere un valore di ritorno.
2. cd[0]= cd non vuol dire nulla visto che manca il NEW.
Curiosità, stai studiando su un libro o stai andando allo sbaraglio?
edit: 3. All'interno del ciclo dovresti usare cd[i] e non cd[1]: ti mostrerà sempre solo il secondo elemento.
4. Cd è un oggetto complesso, quando lo manderai in stampa non vedrai nulla ti interessante: solo il nome della classe seguita da un hash. Devi ridefinire il metodo toString().
File Cd.java:
codice:
public class Cd
{
private String author;
private String title;
private int time;
private double price;
public Cd(String author, String title, int time, double price)
{
this.author = author;
this.title = title;
this.time = time;
this.price = price;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public int getTime()
{
return time;
}
public void setTime(int time)
{
this.time = time;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
@Override
public String toString()
{
return String.format("%s, %s - %d (%f)", title, author, time, price);
}
}
File Collaudo.java:
codice:
public class Collaudo
{
public static void main(String args[])
{
Cd cd[] = new Cd[6];
cd[0] = new Cd("Lady gaga", "The Fame monster", 118, 20.72);
cd[1] = new Cd("Track 2 inediti", "Vasco Rossi", 95, 16.72);
cd[2] = new Cd("if", "Mario biondi", 100, 17.52);
cd[3] = new Cd("Laura live tour", "Laura pausini", 98, 19.50);
cd[4] = new Cd("Crazy love", "Michael bublè", 90, 20.40);
cd[5] = new Cd("this is is", "Michael Jackson", 120, 21.90);
for (int i = 0; i < cd.length; i++)
{
System.out.println(cd[i]);
}
}
}