Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi guys, I have problem on this homework. I need the ID to increase (start from

ID: 664499 • Letter: H

Question

Hi guys, I have problem on this homework. I need the ID to increase (start from 10),but as the instruction asks to use final int ID, I don't know how to deal with it. If I final int ID =10, all the ids are 10. Anybody can help this? I bold the instruction in code.

I need this output:

my code:

public class Shopper {
  
// YOUR CODE: instance variables
public String name;

final int ID = 10;
  
// YOUR CODE: The constructor
  
public Shopper (String name) {
this.name = name;
}
//Give the Shopper a unique 2-digit id (range 10-99, starting at 10). You may find it helpful to //give your class a static variable, shared by all objects in the class, to do this. Assume that you will //not need any IDs past 99.
// YOUR CODE: getName method
public String getName() {
return name;
}

// YOUR CODE: getID method
public int getID(){
return ID;
  
}

// YOUR CODE: setName method
public void setName(String name){
this.name = name;

}
// YOUR CODE: toString method
public String toString(){
return String.format("Name: %s,ID: %d", name, ID);

}
public static void main(String[] args){
  
Shopper firstShopper = new Shopper("Jane Doe");
Shopper secondShopper = new Shopper("John Doe");
Shopper thirdShopper = new Shopper("Rob Doe");
  
System.out.printf("The 1st Shopper's name is %s. ", firstShopper.getName());
System.out.printf("The 2nd Shopper's ID is %d. ", secondShopper.getID());
System.out.print("And the 3rd Shopper... ");
System.out.println(thirdShopper);
System.out.println();
  
System.out.printf("The 2nd Shopper's name was %s... ", secondShopper.getName());
secondShopper.setName("William Shakespeare");
System.out.printf("...but it is now %s. ", secondShopper.getName());
  
}
  
}

Explanation / Answer

// Using Reflection we can access the final or private variables.

import java.lang.reflect.Field; //
public class Shopper
{
  
  
public String name;

final int ID = 10;

  
public Shopper (String name)
{
this.name = name;

}

public String getName()
{
return name;
}

public int getID()
{

return ID+1;
  
}

public void setName(String name)
{
this.name = name;
}

public String toString()
{
return String.format("Name: %s,ID: %d", name, getID());
}

public static void main(String[] args)throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
{

Field FinalField = Shopper.class.getDeclaredField("ID");
   FinalField.setAccessible(true);

Shopper firstShopper = new Shopper("Jane Doe");
Shopper secondShopper = new Shopper("John Doe");

Shopper thirdShopper = new Shopper("Rob Doe");
  
System.out.printf("The 1st Shopper's name is %s. ", firstShopper.getName());
System.out.printf("The 2nd Shopper's ID is %d. ", secondShopper.getID());
System.out.print("And the 3rd Shopper... ");
System.out.println(thirdShopper);
System.out.println();
  
System.out.printf("The 2nd Shopper's name was %s... ", secondShopper.getName());
secondShopper.setName("William Shakespeare");
System.out.printf("...but it is now %s. ", secondShopper.getName());
  
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote