Question: Model a real life object as a Java class with at least one attribute and an instance method. Write main method to create instance

Model a real life object as a Java class with at least one attribute and an instance method.

 

Write main method to create instance of the class, assign a value to the attribute, call your method, and demonstrate that the value of the attribute changed and that the method successfully completed what it was supposed to do.

 


//This is the code I currently have, I am a beginner and would like to know a solution since I can't seem to understand the concepts of non static variables and changing them. Any help would be appreciated, thank you!


public class PC {

//attributes to class PC (temperature, cost, on or off)

int initialtemp= 0;
int temp = 80;
int cost = 1000;
boolean ONOFF = false;

//instance
public void PCTemp(int initialtemp, boolean ONOFF, int temp) {

System.out.println("The PC is currently at: " +initialtemp);
System.out.println("Is the PC turned on?" +ONOFF);
System.out.println("The PC is currently at a retail cost of: " +cost);
}



public static void main(String[] args) {
PC instance = new PC();
instance.ONOFF= true;
instance.temp= 85;

System.out.print(ONOFF);
System.out.print(temp);
}

}

Step by Step Solution

3.28 Rating (151 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public class PC Attributes of the PC instance variables private int temperature 0 Initial temperatur... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!