Intro to Java Programming Can you explain and comment on what\'s happeing in the
ID: 3771546 • Letter: I
Question
Intro to Java Programming
Can you explain and comment on what's happeing in the code below, step-by-step?
public class TestVacation {
public static void main(String[] args) {
Vacation[] vac = new Vacation[2];
vac[0] = new Vacation("Honolulu", 12);
vac[1] = new Vacation("New York", 7);
System.out.printf("%-15s %-10s ", "Destination","Duration" ); for(int i =0; i < vac.length;i++){ System.out.printf("%-15s %-10d ", vac[i].getDest(),vac[i].getDurationInDays());
}
}
}
class Vacation {
private String dest;
private int durationInDays;
public Vacation(String dest, int durationInDays) {
this.dest = dest;
this.durationInDays = durationInDays;
}
public String getDest() {
return dest;
}
public void setDest(String dest) {
this.dest = dest;
}
public int getDurationInDays() {
return durationInDays;
}
public void setDurationInDays(int durationInDays) {
this.durationInDays = durationInDays;
}
}
Explanation / Answer
This below Java Code is having detailed commnets part what is doing each method in step by step..See the below
// Declared Main method class with java file called TestVacation
public class TestVacation {
// Main Method
public static void main(String[] args) {
// Declaring Vacation Class Array of size two
Vacation[] vac = new Vacation[2];
// Initiating Vacation of Class Data Members values with Honolulu and 12 days with first object of Vacation Class
vac[0] = new Vacation("Honolulu", 12);
// Initiating Vacation of Class Data Members values with NewYork and 7 days with Second object of Vacation Class
vac[1] = new Vacation("New York", 7);
//Print the Vacation Class Data Members of Array objects data with using for loop..
// Because It is having two array objects
System.out.printf("%-15s %-10s ", "Destination","Duration" );
for(int i =0; i < vac.length;i++)
{ System.out.printf("%-15s %-10d ", vac[i].getDest(),vac[i].getDurationInDays());
}
}
}
// Declared Classed Called Vacation with two Data Members Called
// String Destination and Duration of holidays as a Integer Variable
class Vacation {
private String dest;
private int durationInDays;
// Default Constructor of Vacation Class with initialising default values to data members
public Vacation(String dest, int durationInDays) {
this.dest = dest;
this.durationInDays = durationInDays;
}
// This method getDest will return the destination place of Class Vacation
public String getDest() {
return dest;
}
// This method getDest will set the Destination of the Class Vacation Destination place with value
public void setDest(String dest) {
this.dest = dest;
}
// This method getDurationInDays will return the class Vacation Data Member durationInDays
public int getDurationInDays() {
return durationInDays;
}
// This method setDurationInDays will Set the class Vacation Data Member durationInDays
public void setDurationInDays(int durationInDays) {
this.durationInDays = durationInDays;
}
} // End of Vacation Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.