package javaapplication1;
import java.io.*;
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
}
//TODO:
//Increase the ‘data’ member of every NotSimple object
//in the array ‘a’ by 1
static void increaseData(NotSimple[] a) {
//TODO: FILL ME
}
//TODO:
//Print the data of every NotSimple object in the array ‘a’
static void printObjData(NotSimple[] a) {
//TODO: FILL ME
}
//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;
}
Run Code Online (Sandbox Code Playgroud)
如果我是对或错的话,我会尝试解释一下我从数组中理解的内容,你们可以向我解释并帮助为我澄清一下吗?提前致谢.
NotSimple[] objArray;这声明了一个数组,objArray = new NotSimple[size];它创建并将数组
的长度分配给objArray?
我的书写道,要声明一个数组,你可以执行以下操作:elementType[] arrayRefVar.据我所知,NotSimple这不是一个元素int double等等.Hhow它是否有效?我可以说任何话吗?
在网站和我的书中,它没有解释是什么x(y);.例如initializeObj(objArray);,那是做什么的?
老师要我做了一些事情,如设置每Notsimple对象数组中a来NotSimple(),但它是很难,如果我甚至不掌握的基本知识.你们能澄清1-3所以我可以学习吗?
在网站和我的书中,它没有解释什么是x(y); 确实.
Java类有一种称为方法的东西.它有时也称为例程,子例程或函数.您发布的代码中的main()是一种方法.
通常,方法是一组明确定义的代码,它执行单个任务:
setBackgroundColor();
clearTemperature();
openValve();
因此,x()将是一种方法,一种(希望)特定的,定义明确的任务.
现在,x(y)意味着"x()想要完成一项任务,但它必须做某事,或者它需要一些东西来完成它的任务"......"y"就是这样.
所以x(y)实际上意味着,"x做了某些事情,并且它希望用y或y做"
例子:
calculateAverageTemperate(temperatureList);
附近的某个地方是计算平均温度的方法.为此,它可能会循环一个温度列表(在您查看代码或javadoc之前,您不会确切地知道它是什么.)
setCheese("巴西海狸");
setCheese()可能会设置一个变量来告诉你午餐时吃的是什么.今天,它是巴西海狸奶酪.
applyUpvote(1);
你投了这个答案!恭喜.在某处,Stack Overflow代码将为此答案的upvote计数添加一个.
当然还有一点比这更多......