pls help!! For the following questions, \"define a class\" means the following .
ID: 3757396 • Letter: P
Question
pls help!!
For the following questions, "define a class" means the following . Create an appropriately-named file containing the Java class definition, including the following - A block comment describing the file (and hence the class), as always Any instance variables, as required by the question - Accessor (getter) and mutator (setter) methods for any instance variables - Constructors as appropriate or as required by the question - A toString method that prints instances of the class informatively Any additional methods as required by the question. All methods other than get ters and setters MUST have a descriptive comment before their definition . Create a separate appropriately-named file containing a different Java class with a main method that tests your new class by doing the following - Create at least two instances of the class. Change at least one of them using the class' methods - Print all the objects informatively If you prefer, you may write one class with a main method to test all the classes defined in this homework. In fact, your TAs would probably appreciate it since it's easier to test. Just make sure it's clear what's going on (good comments in code, good content in output) As always, be sure to document how to run your code and interpret the output in your README.txt fileExplanation / Answer
//Example.java
/*
* File Name: Example.java
*
* Constructor Example() sets x,y to 0
* Constructor Example(int x, int y) sets x,y of Example class to given arguments
* Example class contains two data members x,y as integers.
* mutator classes setters and getters for x and y
* a function to add two numbers which returns sum of two integer numbers
* toString() function returns x,y as string
*/
class Example
{
//data members x,y of integer type
int x,y;
//constructor without arguments
Example()
{
this.x = 0;
this.y = 0;
}
//Constructor with arguments
Example(int x,int y)
{
this.x = x;
this.y = y;
}
//Getter for x
public int getX()
{
return this.x;
}
//Getter for y
public int getY()
{
return this.y;
}
//Setter for x
public void setX(int x)
{
this.x = x;
}
//Setter of y
public void setY(int y)
{
this.y = y;
}
/*
* add() function takes no arguments
* it calculates the sum of data members of class that is x,y and returns
* simply saying add() returns sum of x,y of instance of the class
*/
public int add()
{
return this.x+this.y;
}
//toString returns the data members of the class in a string format;
public String toString()
{
return "x = "+this.x+" y = "+this.y;
}
}
//Tester.java
/*
* FileName: Tester.java
* Tester class contains a main method which is used to test the Example class
* it creates two objects of Example class
* and tests the different functions in Example class
*/
class Tester
{
//Main Function
public static void main(String args[])
{
Example obj1 = new Example();//Object with default constructor
obj1.setX(12);//Calling setter function on obj1 to set value of x to 12
obj1.setY(21);//Calling setter function on obj1 to set value of y to 21
int total = obj1.add();//calling add function of obj1 to add x and y and storing sum of them into total
System.out.println("Object1 Data members -> "+obj1+" Addition = "+total);//obj1 is trasfromed into string because we have create toString(),it returns the toString() string
Example obj2 = new Example(10,20);//object with parameterized constructor
obj2.setX(11);//changing value of obj2.x to 11 using setX() function of obj
int total2 = obj2.add();//calling add function of obj2
System.out.println("Object2 Data members -> "+obj2+" Addition = "+total2);//obj1 is trasfromed into string because we have create toString(),it returns the toString() string
}
}
README.txt
Example.java contains the data members and functions to perform on those data members
Tester.java contains the code to test the Example.java
you don't have to compile Example.java ,if you compile Tester.java it automatically compiles Example.java and creates .class files
Syntax to compile: javac Tester.java
Syntax to run: java Tester
Sample output1: Object1 Data members -> x = 12 y = 21 Addition = 33
Sample output2: Object2 Data members -> x = 11 y = 20 Addition = 31
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.