/*
* 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
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 :
NotSimple nsObj1 = new NotSimple();
prints5
.NotSimple nsObj2 = new NotSimple(50, "Another immutable string!");
prints 50nsObj2 = nsObj1; nsObj2.setData(10); nsObj1.setData(100);
Basically you just said that nsObj2 value is 100 and it prints 100nsObj1 = new NotSimple();
nsObj1 will print 5 (as it's a reference to a new Object)
nsObj2 will still print 100nsObj2 = new NotSimple();
nsObj2 is a reference to a new Object, it will print 5nsObj2.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
The increaseData
method which increments the member field data
of objects in its argument array is called only on the objArray
.
In the method explainOutputs
you don't increment anywhere. You set data
field to different values and print them.
To fix this you can write another overloaded increaseData
method that takes a NotSimple
as:
static void increaseData(NotSimple a) {
a.setData(a.getData()+1);
}
And call this method before you print in any object in the explainOutputs
method as:
NotSimple nsObj1 = new NotSimple();
//1a
increaseData(obj1); // ADD THIS.
System.out.println( "nsObj1.data is\t" + nsObj1.getData() );
System.out.println( "nsObj1.str is \t" + nsObj1.getStr() );
With the above method in the class your existing increaseData
method can make use of it as:
static void increaseData(NotSimple[] a) {
for (int i = 0; i < a.length; i++) {
increaseData(a[i]); // call increaseData for each object in the array.
}
}
精彩评论