Question
Create a class called Methods that has a single instance variable private int x, then write the following six methods in the class: A static
Create a class called Methods that has a single instance variable private int x, then write the following six methods in the class:
A static method called hello() that takes no parameters, returns no value, and prints Hello, world!
An instance (non-static) method called print() that takes no parameters, returns no value, and prints the current value of the x instance variable like this: x is: x
A setter method for the x instance variable, whose parameter name is also x; remember the naming convention for setter methods and their return type! The setter method must guarantee that x is never set to a value less than 0. If the setter argument is negative, leave x unchanged.
A Methods constructor that takes one int parameter called x and calls the setter method to set the value of the x instance variable
A getter method for the x instance variable; remember the parameter and return types for getter methods!
A static void method called print(), whose single parameter is a Methods object, that prints the value of its parameters x instance variable this is method overloading. This method can use either the objects print() or its getter method to do this.
IN JAVA:
public class Methods
{
private int x; // instance variable
/* write the static hello method here */
/* write the instance method print here */
/* write the setter method for x here; do
not set x to a value less than 0 - if
the input parameter is less than 0,
leave x unchanged */
/* write the single Methods constructor here;
use the setter method to set the value of x */
/* write the standard getter method for x here */
/* write the static print method here; use its
input parameter's print method to print the
value of that object's x instance variable */
public static void main(String[] args) // not required for Week 8 Thursday Lab
{
/* DO NOT MODIFY THIS MAIN METHOD */
Methods m = new Methods(42);
hello();
m.print();
m.setX(34);
System.out.println("m.getX: " + m.getX());
print(m);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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