Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1104439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:29:05+00:00 2026-05-17T01:29:05+00:00

/* * To change this template, choose Tools | Templates * and open the

  • 0
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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

The instructor wants me to “Increase the ‘data’ member of every NotSimple object in the array ‘a’ by 1” When I run the program it only increases the first data. For example when I enter 3 I get this:

run:

Enter a number of objects:
3
6
6
6
nsObj1.data is        5
nsObj1.str is         Initialized!
nsObj2.data is        50
nsObj2.str is         Another immutable string!
nsObj2.data is        100
nsObj2.str is         Initialized!
nsObj1.data is        5
nsObj1.str is         Initialized!
nsObj2.data is        100
nsObj2.str is         Initialized!
nsObj2.data is        5
nsObj2.str is         Initialized!
nsObj1.data is        5
nsObj1.str is         Initialized!
nsObj2.data is        10

My question is shouldn’t all the data be increased by 1? i.e. 101, 6, 101, 6, 6, 11

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:29:06+00:00Added an answer on May 17, 2026 at 1:29 am

    You have two main operations here. The first is the creation of an array containing x elements, each of this element is initialized with the value 5 and get incremented by one. This give you this result :

    3
    6
    6
    6

    3 elements, with the value 5, incremented.

    The second part of your code (the explainOutputs()) doesn’t increment anything. Two objects are declared (nsObj1 and nsObj2), and they’re modified manually before you print them. No incrementation. It just print what you’ve set.

    Here is what you’ve done :

    1. NotSimple nsObj1 = new NotSimple();
      prints 5.
    2. NotSimple nsObj2 = new NotSimple(50, "Another immutable string!");
      prints 50
    3. nsObj2 = nsObj1; nsObj2.setData(10); nsObj1.setData(100);
      Basically you just said that nsObj2 value is 100 and it prints 100
    4. nsObj1 = new NotSimple();
      nsObj1 will print 5 (as it’s a reference to a new Object)
      nsObj2 will still print 100
    5. nsObj2 = new NotSimple();
      nsObj2 is a reference to a new Object, it will print 5
    6. nsObj2.setData(10);
      nsObj1 still prints 5
      nsObj2 will print 10 for because only the object referenced by nsObj2 will have a value changed

    Result :
    5 50 100 5 100 5 5 10

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to change this: <a href='foo'> <div> Moo </div> </a> to be standards
My php is weak and I'm trying to change this string: http://www.example.com/backend.php?/c=crud&m=index&t=care ^ to
I can set data in JTable constructor, and then user can change this data
This problem occurred during daylight saving time change. After the change occurred, we've noticed
I have this code: myVariable which I want to change into trace(myVariable: + myVariable);
I have this in my installer and I need to change the name of
I wanted to change the resolution of a image,this image I am getting from
You can change the connection string at run-time like this. You make the connection
According to this website , you can change to command key sequence used by
This maybe a stupid question, but as I can not easily undo my change

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.