2010-10-02 4 views
0
package javaapplication1; 
import java.io.*; 
/** 
* 
* @author simon 
*/ 
public class Main { 


    /** 
    * @param args the command line arguments 
    */ 
     public static void main(String[] args) throws IOException { 
      NotSimple[] objArray; 
       BufferedReader stdin = new BufferedReader(
            new InputStreamReader(System.in)); 
      System.out.println("Enter a number of objects:"); 

      int size; 
      size = Integer.parseInt(stdin.readLine()); 

      //Initialize objArray 
      objArray = new NotSimple[size]; 

      //TODO: Implement following functions 

      initializeObj(objArray); 
      increaseData(objArray); 
      printObjData(objArray); 

      //TODO: Explain all outputs of the below function 
      explainOutputs(); 
      return; 

                   } 

     //TODO 
     //initialize every Notsimple object in the array 'a' 
     //to NotSimple() 
     //Hint: using the for loop, assign a[i] = new NotSimple(); 
     static void initializeObj(NotSimple[] a){ 
     //TODO: FILL ME 

      for (int i = 0; i < a.length; i++) 
      { 
      a[i] = new NotSimple(); 
      } 

    } 

    //TODO: 
    //Increase the ‘data’ member of every NotSimple object 
    //in the array ‘a’ by 1 
    static void increaseData(NotSimple[] a) { 
     //TODO: FILL ME 

     for (int i = 0; i < a.length; i++) 
     { 
      a[i].setData(a[i].getData()+1); 
     } 

    } 

    //TODO: 
    //Print the data of every NotSimple object in the array ‘a’ 
    static void printObjData(NotSimple[] a) { 
     //TODO: FILL ME 
     for (int i = 0; i < a.length; i++) 
      { 
      System.out.println (a[i].getData()); 
      } 

    } 

    //TODO explain all the outputs 1a-1f 
    static void explainOutputs() { 
    NotSimple nsObj1 = new NotSimple(); 
    //1a 
    System.out.println("nsObj1.data is\t" + nsObj1.getData()); 
    System.out.println("nsObj1.str is \t" + nsObj1.getStr()); 

    NotSimple nsObj2 = new NotSimple(50, 
         "Another immutable string!"); 
    //1b 
    System.out.println("nsObj2.data is\t" + nsObj2.getData()); 
    System.out.println("nsObj2.str is \t" + nsObj2.getStr()); 

    nsObj2 = nsObj1; 

    nsObj2.setData(10); 
    nsObj1.setData(100); 
    //1c 
    System.out.println("nsObj2.data is\t" + nsObj2.getData()); 
    System.out.println("nsObj2.str is \t" + nsObj2.getStr()); 

    nsObj1 = new NotSimple(); 
    //1d 
    System.out.println("nsObj1.data is\t" + nsObj1.getData()); 
    System.out.println("nsObj1.str is \t" + nsObj1.getStr()); 
    System.out.println("nsObj2.data is\t" + nsObj2.getData()); 
    System.out.println("nsObj2.str is \t" + nsObj2.getStr()); 

    nsObj2 = new NotSimple(); 
    //1e 
    System.out.println("nsObj2.data is\t" + nsObj2.getData()); 
    System.out.println("nsObj2.str is \t" + nsObj2.getStr()); 

    nsObj2.setData(10); 
    //1f 
    System.out.println("nsObj1.data is\t" + nsObj1.getData()); 
    System.out.println("nsObj1.str is \t" + nsObj1.getStr()); 
    System.out.println("nsObj2.data is\t" + nsObj2.getData()); 
    System.out.println("nsObj2.str is \t" + nsObj2.getStr()); 
    } 

} 


class NotSimple 
{ 
    NotSimple() 
    { 
    data = 5; 
    str = new String("Initialized!"); 
    } 

    NotSimple(int i, String str1) 
    { 
    data = i; 
    str = str1; 
    } 

    void setData(int i) 
    { 
    data = i; 

    return; 
    } 

    int getData() 
    { 
    return data; 
    } 

    void setStr(String str1) 
    { 
    str = str1; 

    return; 
    } 

    String getStr() 
    { 
    return str; 
    } 

    private int data; 
    private String str; 
} 

Je veux ajouter 1 à la matrice, mais cela ne fonctionne pas. Je reçois l'erreur de compilation suivante:Pouvez-vous ajouter un nombre à un nombre dans un tableau?

operator ++ cannot be applied to javaapplication1.NotSimple 
+0

Est-ce l'erreur correcte mesasge? Cela ne correspond pas au code. –

Répondre

2

Utilisez

for (int i = 0; i < a.length; i++) 
    { 
     a[i].setData(a[i].getData()+1); 
    } 

Depuis Java ne supporte pas la surcharge de l'opérateur, vous ll doit récupérer l'ancienne valeur (c'est-à-dire l'appel getData()), lui ajouter 1 et la définir comme nouvelle valeur (c'est-à-dire l'appel setData()).

+0

@Hippo L'opérateur ++ ne peut pas être appliqué à javaapplication1.NotSimple – CuriousStudent

+0

Veuillez voir la réponse éditée. – AbdullahC

+0

hm ok pas de codes d'erreur. Je vous remercie! BTW les impressions printobjdata sur cette [email protected] [email protected] [email protected] [email protected] cela signifie que je l'ai fait quelque chose de mal hein? – CuriousStudent

0

À quoi ressemble la classe NotSimple?
Est-ce que NotSimple est un type numérique qui peut être incrémenté ainsi?

0

Si NotSimple a un champ appelé 'données' puis remplacez un [i] ++ avec [i] .data ++

+0

données a un accès privé :( – CuriousStudent

+0

@Burleigh Ours je reçois cette erreur :( – CuriousStudent

+0

Si 'un [i] .data ++' travaillé, cela signifierait que NotSimple.data est exposé au monde.Non 'NotSimple.incrementCount() 'ou' NotSimple.addToCount (1) 'être mieux? –

Questions connexes