/*
* 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;
}
教师要我“将数组'a'中每个NotSimple对象的'data'成员增加1”当我运行程序时,它只会增加第一个数据。例如,当我输入3时,我得到了这个:
运行:
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
我的问题是不应该将所有数据增加1吗?即101,6,101,6,6,11
答案 0 :(得分:2)
这里有两个主要操作。第一个是创建包含x
个元素的数组,每个元素都使用值5
进行初始化并增加1。这给你这个结果:
3
6
6
6
3个元素,值为5,递增。
代码的第二部分(explainOutputs()
)不会增加任何内容。声明了两个对象(nsObj1
和nsObj2
),并在打印它们之前手动修改它们。没有增量。它只打印您设置的内容。
以下是您的所作所为:
NotSimple nsObj1 = new NotSimple();
5
。NotSimple nsObj2 = new NotSimple(50, "Another immutable string!");
nsObj2 = nsObj1; nsObj2.setData(10); nsObj1.setData(100);
基本上你只是说nsObj2值是100并且它打印100 nsObj1 = new NotSimple();
nsObj2 = new NotSimple();
nsObj2.setData(10);
结果: 5 50 100 5 100 5 5 10
答案 1 :(得分:2)
仅在increaseData
上调用data
方法,该方法增加其参数数组中对象的成员字段objArray
。
在方法explainOutputs
中,您不会在任何地方增加。您将data
字段设置为不同的值并打印它们。
要解决此问题,您可以编写另一个需要increaseData
的重载NotSimple
方法:
static void increaseData(NotSimple a) {
a.setData(a.getData()+1);
}
在打印explainOutputs
方法中的任何对象之前调用此方法:
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() );
使用上面的方法,您现有的increaseData
方法可以将其用作:
static void increaseData(NotSimple[] a) {
for (int i = 0; i < a.length; i++) {
increaseData(a[i]); // call increaseData for each object in the array.
}
}