Question

Question 2 (3 mark): Write a program to implement the class Coffee according to the following UML diagram and description soQuestion 3 (2 mark) : Write a class to compute the property cost according to the following UML diagram and expressions. The

0 0
Add a comment Improve this question Transcribed image text
Answer #1

check out the solution.Programming in JAVA language.

-----------------------------------------

CODE :

Question 2:

public class Coffee {
// instance attributes
double energy;
double protein;
double fat;
// default constructor
Coffee() {
this.energy = 50.0;
this.protein = 1.5;
this.fat = 1.7;
}
// parameterized constructor
Coffee(double energy, double protein, double fat) {
this.energy = energy;
this.protein = protein;
this.fat = fat;
}
// copy constructor
Coffee(Coffee sourceCup) {
this.energy = sourceCup.energy;
this.protein = sourceCup.protein;
this.fat = sourceCup.fat;
}
// print method implementation
void printNutritionInformation() {
System.out.println("It contains " + this.energy + " Cal energy, " + this.protein + " g protein and " + this.fat + " g fat per serving.");
}
public static void main(String args[]) {
// 3 objects created as instructed
Coffee coffee1 = new Coffee();
Coffee coffee2 = new Coffee(72.0, 1.8, 1.6);
Coffee coffee3 = new Coffee(coffee2);
  
// print information of all objects by calling printNutritionInformation method
coffee1.printNutritionInformation();
coffee2.printNutritionInformation();
coffee3.printNutritionInformation();
} // main ends
} // class ends

---------------------------------------------------------------

9 1- public class Coffee { 2 // instance attributes 3 double energy; 4 double protein; 5 double fat; 6 // default constructor

----------------------------

OUTPUT :

Result compiled and executed in 0.936 sec(s) It contains 50.0 Cal energy, 1.5 g protein and 1.7 g fat per serving. It contain

---------------------------------------------------------

Question 3:

public class PropertyCost {
// class attributes with default values
// private is like read only which can be used by the class only
private static double RATE = 1.1;
private static double UNITPRICE = 1.4;
private static double GST = 0.1;
// main method
public static void main(String args[]) {
System.out.println("Enter the area : ");
// command line argument and convert to double type
double area = Double.parseDouble(args[0]);
// call method and save the returned result in a variable
double cost = calcTotalCharges(area);
// print the result
System.out.printf("Property cost : %.2f", cost);
}
// method definition
public static double calcTotalCharges(double area) {
// expressions mentioned as given in requirement
double actualArea = area * RATE;
double propertyCharge = actualArea * UNITPRICE;
double propertyCost = propertyCharge + propertyCharge * GST;
// return result to main method
return propertyCost;
}
}

--------------------------------------

1- 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class Property Cost { // class attributes with d

------------------------------------------

OUTPUT :

Command Line Arguments 12.17 Result CPU Time: 0.20 sec(s), Memory: 32664 kilobyte(s) Enter the area : Property cost : 20.62

-------------------------------------------------

NOTE : OUTPUT SHOWN ABOVE IS EXECUTED IN ONLINE COMPILER . OUTPUT AND COMMAND LINE ARGUMENTS FORMAT MAY VARY DEPENDING ON COMPILER.

-----------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
Question 2 (3 mark): Write a program to implement the class Coffee according to the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 1 (4 mark): Implement a program with two classes according to the following UML diagram:...

    Question 1 (4 mark): Implement a program with two classes according to the following UML diagram: College -firstLab Student: StudentAccount - secondLab Student: StudentAccount +main (String (1) : void +College (String, String, int, int) : +printStudents(): void contains StudentAccount -name: String -studentNumber: int StudentAccount (String, int): +getName(): String +getStudentNumber(): int 2 REQUIREMENTS • The constructor College (String, String, int, int) shall create two component objects of type StudentAccount, i.e., the first lab student and the second lab student, and initialize...

  • please use c++ Write a program that contains a class Rectangle with two private double precision...

    please use c++ Write a program that contains a class Rectangle with two private double precision members iLength and iWidth, public set and get member functions for these two members, a two argument constructor that sets the length and width to any two user specified values and a void function area() that computes the area and then prints this value by inserting it into the cout output stream. Write a main function that ereates a Rectangle with a length of...

  • Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following...

    Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: · Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. · A no-arg constructor that creates a default rectangle. · A constructor that creates a...

  • Question 1 1 pts Which of the following is not a valid class name in Java?...

    Question 1 1 pts Which of the following is not a valid class name in Java? O MyClass MyClass1 My_Class MyClass# Question 2 1 pts Which of the following statements is False about instance variables and methods? Instance variables are usually defined with private access modifier. Instance variables are defined inside instance methods. Instance methods are invoked on an object of the class that contains the methods. A class can have more than one instance variables and methods. Question 3...

  • Design and implement class Circle to represent a circle object. The class defines the following attributes...

    Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1.      A private variable of type double named radius to represent the radius. Set to 1. 2.      Constructor Methods: •        Python : An overloaded constructor method to create a default circle. •        C# & Java: Default Constructor with no arguments. •        C# & Java: Constructor method that creates a circle with user-specified radius. 3.      Method getRadius() that returns the radius. 4.     ...

  • In C++ please. 7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the Food Item class (in...

    In C++ please. 7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the Food Item class (in files Foodltem.h and Fooditem.cpp) with constructors to initialize each food item. The default constructor should initialize the name to "None" and all other fields to 0.0. The second constructor should have four parameters (food name, grams of fat, grams of carbohydrates, and grams of protein) and should assign each private field with the appropriate parameter value. Ex: If the input is: M&M's 10.0 34.0...

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

  • This question is about java program. Please show the output and the detail code and comment.And...

    This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...

  • In C++, Step 1: Implement the Student Class and write a simple main() driver program to...

    In C++, Step 1: Implement the Student Class and write a simple main() driver program to instantiate several objects of this class, populate each object, and display the information for each object. The defintion of the student class should be written in an individual header (i.e. student.h) file and the implementation of the methods of the student class should be written in a corresponding source (i.e. student.cpp) file. Student class - The name of the class should be Student. -...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT