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.s开发者_高级运维tr 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'll attempt to explain what little I understand from array can you guys/girls explain to me if I am right or wrong and help clarify it for me? Thanks in advance.
NotSimple[] objArray;
this declares an arrayobjArray = new NotSimple[size];
this creates and assign the length of the array to objArray?My book writes that to declare an array you do the following:
elementType[] arrayRefVar
. As far as I knowNotSimple
is not an element such asint
double
etc. Hhow does it work? Can I just put any words?On websites and in my book it doesn't explain what
x(y);
does. For exampleinitializeObj(objArray);
What does that do?
The teacher wants me to do a number of things such as initialize every Notsimple object in the array a
to NotSimple()
but it is hard if I don't even grasp the basics. Can you guys clarify 1-3 so I can learn?
On websites and in my book it doesn't explain what x(y); does.
Java classes have something called a method. It's also called a routine, subroutine, or function sometimes. The main() in the code you posted is a method.
In general, a method is a well-defined bunch of code that performs a single task:
setBackgroundColor();
clearTemperature();
openValve();
So, x() would be a method, a (hopefully) specific, well-defined task.
Now, x(y) means "x() wants to do a task, but it has to do it to something, or it needs something in order to do its task" ... "y" is the thing.
So x(y) really means, "x does something, and it wants to do it to, or with, y"
Examples:
calculateAverageTemperate(temperatureList);
Somewhere nearby is a method that calculates an average temperature. To do so it will likely loop over a list of temperatures (you won't know exactly what until you look at the code or javadocs.)
setCheese("brazilian beaver");
setCheese() probably sets a variable telling what you're having for lunch. And today, it's Brazilian Beaver Cheese.
applyUpvote(1);
You voted for this answer! Congrats. Somewhere, the Stack Overflow code will add one to this answer's upvote count.
There a little more to it than this of course...
This line as you say create an array of items.
objArray = new NotSimple[size];
However the array doesn't have any items in it, its just space to hold references to a set of new objects. You need to create these objects, give them appropriate information and put them in the array, in this function (in other word initialise the array)
initializeObj(objArray);
Ensure that when increaseData is called, the contents of the array are ready to have this called on them.
It seems to me NotSimple is a class which you must create yourself for this assignment.
You're reading in from System input, so when the program is ran you're asking the user for input which becomes the size of the array of NotSimple objects.
You are to create a class called NotSimple and then you can call NotSimple[] arrayOfNotSimple. This works in the same way as int[], String[] etc.
You have to implement it yourself, it seems to me you are required to initialize size NotSimple objects in the array. Which is dependent on how you set up the NotSimple class, it could be as simple as a[0] = new NotSimple()
1 & 2 - NotSimple is a class defined somewhere else. You are essentially creating an array of NotSimple objects.
You are assigning the max number of elements the array can hold based on user input.
3 - initializeObj(objArray) is calling the initializeObj method, using your array as a parameter.
The initializeObj method will fill that array with NotSimple objects (based on the name and comments)
1) NotSimple[] objArray
declares a variable objArray
of type NotSimple[]
(an array of NotSimple
s). There is no data associated with objArray
yet -- it's null
. new NotSimple[size]
actually creates the array of NotSimple
s, although each slot in the array is null
(think of it as empty). The objArray =
before it assigns the newly created array to the objArray
variable.
2) Types can be primitives (like int, double) or objects of a particular class. NotSimple
is a class presumably defined elsewhere. So no, you can't just put any word there, it has to be something defined. Your assignment has a rather contrived example, so I can see why you're confused.
3) I'm quite sure your book does define what x(y)
does. That is a function call (technically method call but for now you can treat them the same).
If you don't think my answer is clear, just leave a comment.
You are correct. What it does is allocate space for
size
pointers, initially pointing tonull
, that you can replace with pointers to actual objects by saying something likeobjArray[5] = someObject;
. Pointers are a topic you probably haven't covered yet, so I apologize if that just made it more confusing.NotSimple
is a class. This is very big part of Java (and object oriented programming in general) and your book will definitely devote a lot of time to the concept, even if it hasn't yet. It is like a simple type,int
etc, but more complicated. It can contain multiple values and also methods that can act on those fields..That is the syntax for calling a method. In the case of
x(y);
, that means "call the method calledx
with the parametery
." You can also call methods with multiple parameters by separating them with commas, like so:nameOfTheMethod(parameterOne, parameterTwo, parameterThree)
.
You didn't ask about this, but in case it helps:
NotSimple someObject = new NotSimple();
Is a call to the constructor. This is a special method of a class that, basically, creates a new NotSimple
object and returns it. Constructors are special because they must be combined with the new
keyword, they have the same name as the class they're constructing, and they have no return type (although the last point won't matter until you start writing your own constructors).
精彩评论