Intro to Java Programming Can you explain or comment on what happens in the code
ID: 3771541 • Letter: I
Question
Intro to Java Programming
Can you explain or comment on what happens in the code below, step-by-step?
public class InvItem {
private static int curID = 1;
private int id;
private String loc;
public InvItem() {
id = -1;
loc = "";
}
public InvItem(String loc) {
this.id = curID;
this.loc = loc;
curID++;
}
public int getId() { return id; }
public String getLoc() { return loc; }
public void setLoc(String loc) { this.loc = loc;}
public static void main(String[] args){
InvItem[] items = new InvItem[4];
items[0] = new InvItem("B1");
items[1] = new InvItem("FRONT");
items[2] = new InvItem("SHIPPING");
items[3] = items[2];
items[2].setLoc("QC HOLD");
System.out.println("Item ID: " + items[0].getId() + " Items Location: " + items[0].getLoc()); System.out.println("Item ID: " + items[1].getId() + " Items Location: " + items[1].getLoc()); System.out.println("Item ID: " + items[2].getId() + " Items Location: " + items[2].getLoc()); System.out.println("Item ID: " + items[3].getId() + " Items Location: " + items[3].getLoc());
}
}
Explanation / Answer
/*
Java Class InvItem
This Java class InvItem describes how class is defined and being used
in Java language.
Syntax of defining java class is,
<modifier> class <class-name>{
// members and methods
}
*/
public class InvItem {
/*
Syntax of defining memebers of the java class is,
<modifier> type <name>;
here InvItem is class name
*/
private static int curID = 1;/*
Syntax of defining memebers of the java class is,
<modifier> type <name>;
*/
private int id;//here private is modifier int is datatype and id is name of memeber/variable of class
private String loc;//here private is modifier String is datatype and loc is name of memeber of class
public InvItem()
{
id = -1;//id is variable or data member whose value is -1
loc = "";
}
public InvItem(String loc) {
this.id = curID;// this keyword represent current instance of class.
this.loc = loc;/*The this() constructor call should be used to reuse the constructor in the constructor.
It maintains the chain between the constructors i.e. it is used for constructor chaining. */
curID++;
/*
* Java increment operator ++ increases its operand's value by one
* while decrement operator -- decreases its operand's value by
* one as given below.
*/
}
/*
Syntax of defining methods of the java class is,
<modifier> <return-type> methodName(<optional-parameter-list>) <exception-list>{
...
}
*/
public int getId()
{
return id;/return the id
}
public String getLoc() { return loc; }
public void setLoc(String loc)
//set passed parameter as name
{ this.loc = loc;}
public static void main(String[] args) //main method will be called first when program is executed
{
/*
Syntax of java object creation is,
<class-name> object-name = new <class-constructor>;
*/
InvItem[] items = new InvItem[4];
items[0] = new InvItem("B1");
items[1] = new InvItem("FRONT");
items[2] = new InvItem("SHIPPING");
items[3] = items[2];
items[2].setLoc("QC HOLD");
// It will print the value of Item ID,ItemLocation according to the array value
System.out.println("Item ID: " + items[0].getId() + " Items Location: " + items[0].getLoc()); //will print Item ID: 1 Items Location: B1
System.out.println("Item ID: " + items[1].getId() + " Items Location: " + items[1].getLoc());//will print Item ID: 2 Items Location: FRONT
System.out.println("Item ID: " + items[2].getId() + " Items Location: " + items[2].getLoc()); //will print Item ID: 3 Items Location: QC HOLD
System.out.println("Item ID: " + items[3].getId() + " Items Location: " + items[3].getLoc());//will print Item ID: 3 Items Location: QC HOLD
}
}
output:
Item ID: 1 Items Location: B1
Item ID: 2 Items Location: FRONT
Item ID: 3 Items Location: QC HOLD
Item ID: 3 Items Location: QC HOLD
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.