The following table provides the characteristics of the fields that are used in
ID: 3846219 • Letter: T
Question
The following table provides the characteristics of the fields that are used in the above two classes.
Field name
Class
Access specifier
Data type
Name
Expe
nse
private
String
Amount
Expe
nse
private
double
GSTInclud
ed
Expe
nse
private
boolean
GSTAmount
GSTIt
em
private
double
You need to write (define) the above mentioned two Java classes. Your classes should have appropriate fields/variables, constructors, getters and setters methods and overridden toString methods.
Field name
Class
Access specifier
Data type
Name
Expe
nse
private
String
Amount
Expe
nse
private
double
GSTInclud
ed
Expe
nse
private
boolean
GSTAmount
GSTIt
em
private
double
Explanation / Answer
//Class Expense definition
class Expense
{
//Instance variables
private String Name;
private double Amount;
private boolean GSTIncluded;
//Parameterized constructor
Expense(String na, double am, boolean gst)
{
Name = na;
Amount = am;
GSTIncluded = gst;
}//End of constructor
//Method to set name
void setName(String na)
{
Name = na;
}//End of method
//Method to set Amount
void setAmount(double am)
{
Amount = am;
}//End of method
//Method to set gst included status
void setGSTIncluded(boolean gst)
{
GSTIncluded = gst;
}//End of method
//Method to return Name
String getName()
{
return Name;
}//End of method
//Method to return Amount
double getAmount()
{
return Amount;
}//End of method
//Method to return Gst included status
boolean getGSTIncluded()
{
return GSTIncluded;
}//End of method
//Overrides toString method
public String toString()
{
//Initializes the message to null
String msg = "";
//Concatenates the output
msg += " Name: " + Name + " Amount: $" + Amount + " GST Included: " + GSTIncluded;
//Returns the string
return msg;
}//End of method
}//End of class
//Defines class GSTItem
class GSTItem
{
//Instance variable
private double GSTAmount;
//Parameterized constructor
GSTItem(double am)
{
GSTAmount = am;
}//End of constructor
//Method to set GST Amount
void setGSTAmount(double am)
{
GSTAmount = am;
}//End of method
//Method to return GST Amount
double getGSTAmount()
{
return GSTAmount;
}//End of method
//Overrides the toString() method
public String toString()
{
String msg = "";
msg += " GST Amount: $" + GSTAmount;
return msg;
}//End of method
}//End of class
//Driver class
public class GSTItemDemo
{
//Main method
public static void main(String ss[])
{
//Creates an object of Expense class
Expense e1 = new Expense("Pyari", 1000, true);
System.out.println(e1);
//Creates an object of GSTItem class
GSTItem g1 = new GSTItem(4000);
System.out.println(g1);
}//End of method
}//End of class
Output:
Name: Pyari
Amount: $1000.0
GST Included: true
GST Amount: $4000.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.