tags:

views:

125

answers:

3
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;
}

I want to add 1 to the array but it doesn't work. I get the following compilation error:

operator ++ cannot be applied to javaapplication1.NotSimple
A: 

What does the NotSimple class look like?
Is NotSimple a numeric type that can be incremented so?

Thogek
A: 

If NotSimple has a field called 'data' then replace a[i]++ with a[i].data++

Burleigh Bear
data has private access :(
CuriousStudent
@Burleigh Bear i get that error :(
CuriousStudent
If `a[i].data++` worked it would mean that NotSimple.data is exposed to the world. Wouldn't `NotSimple.incrementCount()` or `NotSimple.addToCount(1)` be better?
Tony Ennis
@CuriousStudent - you WANT data to remain private. It's a very good thing. Don't expose the details of a class to another class. Instead, add a _method_ to NotSimple that performs the task you want.
Tony Ennis
@Tony Ennis does this work also? it doesn't seem to give me a code a[i].setData(a[i].getData()+1);
CuriousStudent
The solution you quote is the more 'canonical' way. There's a common technique in java where the code has one-line methods that are called "setters" and "getters". The setters set a class's values. The getters return values. That solution uses the very general purpose setters and getters to accomplish the task safely and responsibly. Your IDE will likely generate your setter and getter code for you :-D Using the setter and getter combo, or using a special purpose method, is a matter of taste. The methods I suggest above are cleaner looking but useless if you want to add 5.
Tony Ennis
@Tony EnnisEnter a number of objects:266nsObj1.data is 5nsObj1.str is Initialized!nsObj2.data is 50nsObj2.str is Another immutable string!nsObj2.data is 100nsObj2.str is Initialized!nsObj1.data is 5nsObj1.str is Initialized!nsObj2.data is 100nsObj2.str is Initialized!nsObj2.data is 5nsObj2.str is Initialized!nsObj1.data is 5nsObj1.str is Initialized!nsObj2.data is 10nsObj2.str is Initialized!BUILD SUCCESSFUL (total time: 3 seconds)something is wrong huh?
CuriousStudent
@Tony Ennis I think my program is suppose to add 1 to every data?
CuriousStudent
+2  A: 

Use

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

Since Java does not support operator overloading, you'll need to retrieve the old value (i.e. the getData() call), add 1 to it, and set it as the new value (i.e. the setData() call).

Hippo
@Hippo operator ++ cannot be applied to javaapplication1.NotSimple
CuriousStudent
Please see the edited answer.
Hippo
hm okay no error codes. thank you! btw the printobjdata prints out this [email protected]@42e816javaapplication1.NotSimple@addbf1javaapplication1.NotSimple@42e816this means i did something wrong huh?
CuriousStudent
You should use System.out.println(a[i].getData()) instead of System.out.println(a[i]) in your print function.
Hippo
@Hippo Enter a number of objects:266nsObj1.data is 5nsObj1.str is Initialized!nsObj2.data is 50nsObj2.str is Another immutable string!nsObj2.data is 100nsObj2.str is Initialized!nsObj1.data is 5nsObj1.str is Initialized!nsObj2.data is 100nsObj2.str is Initialized!nsObj2.data is 5nsObj2.str is Initialized!nsObj1.data is 5nsObj1.str is Initialized!nsObj2.data is 10nsObj2.str is Initialized!BUILD SUCCESSFUL (total time: 3 seconds)something is still wrong :(
CuriousStudent
If you tell us what exactly you think is wrong, and what you expect, then we can help better :)
Hippo
@Hippo sorry lol but it didn't add 1 to all the data, it only added 1 to my input....so something is wrong with the input module.
CuriousStudent
maybe its right? haha I can't seem to interpret what my teacher want
CuriousStudent
@Hippo //TODO: //Increase the ‘data’ member of every NotSimple object //in the array ‘a’ by 1
CuriousStudent
I'm not sure what explainOutputs() is supposed to do, but if you want to see how the 'data' member changes, call printObjData() before calling increaseData(), as well as after it to see the difference.
Hippo
@Hippo hm so explainOutputs() have nothing to do with all the lines above right?
CuriousStudent
nvm it does. when i delete it the program doesn't run
CuriousStudent
Comment out the call to explainOutputs(), and see what does not get printed. Don't delete the function itself, only comment out the call to it from main().
Hippo
@Hippo run:Enter a number of objects:266BUILD SUCCESSFUL (total time: 2 seconds)when i enter 2
CuriousStudent
@Hippo I think something is wrong with the static void increaseData(NotSimple[] a) { //TODO: FILL ME for (int i = 0; i < a.length; i++) { a[i].setData(a[i].getData()+1); }}it doesn't add 1 to every data, only to the first one
CuriousStudent
Now add the call to printObjData() before increaseData()
Hippo
All the data fields have a value of 5, so when you add 1, all become 6.
Hippo
@Hippo 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() );shouldn't that data have 50?
CuriousStudent
You commented out the call to explainOutputs(), so that code doesn't run. Only the code inside NotSimple's default constructor will run, which is called from the initializeObj() method.
Hippo
hm...but i didn't write that code, so i think its suppose to be there. The only codes that I wrote are after "todo fill me"
CuriousStudent