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
Step: 1
public class PC Attributes of the PC instance variables private int temperature 0 Initial temperatur...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started