Given a class called Product,
Product |
---|
- id : String - price : double |
+ Product() + Product(id : String, price : double) + getID() : String + getPrice() : double + toString() : String |
Assume its toString method will return a string like this:
ID: id of this obj
Price: $xxx.xx
Write a FreshProduce class which extends Product. All instance data members should be private and all constructors and methods should be public unless specified otherwise. This FreshProduce class should:
class Product // base class
{
private String id;
private double price;
//constructors
public Product()
{
id = "";
price = 0.0;
}
public Product(String id, double price)
{
this.id = id;
this.price = price;
}
//get methods
public String getID()
{
return id;
}
public double getPrice()
{
return price;
}
public String toString()
{
return "ID : "+getID() +
String.format("\nprice :%.2f",getPrice());
}
}
class FreshProduce extends Product // derived class
{
private String expirationDate;
public FreshProduce()
{
super();// call to base class
constructor
expirationDate = "";
}
public FreshProduce(String id, double price, String
expirationDate)
{
super(id,price); // call to base
class constructor
this.expirationDate =
expirationDate;
}
public String toString()
{
return super.toString()
+"\nExpiration Date : "+expirationDate;
}
}
class Test
{
public static void main (String[] args)
{
FreshProduce tomato = new
FreshProduce("A-123",10.55,"February 20, 2020");
System.out.println(tomato);
}
}
Output:
ID : A-123 price :10.55 Expiration Date : February 20, 2020
Do ask if any doubt. Please up-vote.
Given a class called Product, Product - id : String - price : double + Product()...
Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double +setPrice(double): void +toString(): String The class has two attributes, title and price, and get/set methods for the two attributes. The first constructor doesn’t have a parameter. It assigns “” to title and 0.0 to price; The second constructor uses passed-in parameters to initialize the two attributes. The toString() method returns values for the two attributes. Notation: - means private, and + means public. 1....
Need help to create general
class
Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...
Ticket Hierarchy Ticket Abstract Class Create a static variable called nextTicketId. This is an integer representing the next available integer for the ticketId private static int nextTicketId = 1000; getPrice method: Abstract method that returns a double. NOTE: Do not make price an instance variable in Ticket. Noargument constructor: Sets event name to “none”, event location to “none”, and ticket id to 0. Create a constructor that accepts the event name and event location as parameters. Set the ticket Id...
Question #1 (CLO: 1,2.3) Make a class called Car. Declate its data, model, colour, price Also make a constructor to initialize these data variables. Make a getPrice() method to return price. Make a Child class of Car, called Truck. It has a data variable, weight. Make a constructor that takes all the data variables including weight and those of Car class. Also override getPrice() method, which return 20% discount price if the weight is more than 20,000 else it returns...
C# programming
50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...
Create an Item class, which is the abstract super class of all Items. Item class includes an attribute, description of type String for every item. [for eg. Book] A constructor to initialize its data member of Item class. Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details. Declare a constant RATE with value 0.25 Declare a method called calculateExtraCharge(), which returns a double value. Create the...
Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed to the constructor Methods: Accessors int getID( ) String getName( ) Class Roster: This class will implement the functionality of all roster for school. Instance Variables a final int MAX_NUM representing the maximum number of students allowed on the roster an ArrayList storing students Constructors a default constructor should initialize the list to empty strings a single parameter constructor that takes an ArrayList<Student> Both...
Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String in the form yyyymmdd (such as 20161001 for October 1, 2016), so you will need three private instance variables. Two constructors should be provided, one that takes three integer parameters, and one that takes a String. The constructor with the String parameter and should validate the parameter. As you are reading the dates you should check that...
Create the class Book which has the following members: 1. private members: 1. title as String 2. isbn as String 3. author as String 4. price as double 2. public members: 1. default constructor which initializes all data members to their default values. 2. non-default constructor which initializes all data members to parameters values. 3. toString which returns a representing String of the Book. 4. add all the setters and getters. Create the class EBook which is a subclass of...
Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...