This is the Program I am try create !
Create a class CoffeeCup with a double instance variable temperature. Provide a parameterized constructor, an accessor/getter, and a mutator/setter. It may be noted that 75 degree Celsius is considered best temperature for coffee to be served for drinking.

Create another class TeaCup with a double instance variable temperature. Provide a parameterized constructor, an accessor/getter, and a mutator/setter. It may also be noted that 70 degree Celsius is considered best temperature for tea to be served for drinking.

Create TemperatureException, TooColdException, and TooHotException classes as drawn in the exception hierarchy below. Each exception class should include a default constructor and a parameterized constructor with one String parameter. Also override toString method in each exception class to return description of exception.


Create another class VirtualPerson with methods drinkCoffee(CoffeeCup acup) and drinkTea(TeaCup acup), each of which should throw TooColdException or TooHotException respectively if the coffee or tea to be served is either too cold or too hot. 65 degree Celsius should be considered too cold temperature for coffee and 85 degree Celsius should be considered too hot temperature for coffee. Similarly, 60 degree Celsius should be considered too cold temperature for tea and 80 degree Celsius should be considered too hot temperature for tea.

Create another class VirtualCafe with a static method serveCustomer(VirtualPerson customer, CoffeeCup acup) overloaded as serveCustomer(VirtualPerson customer, TeaCup acup). First version should invoke drinkCofee and second should invoke drinkTea method of VirtualPerson from within a try block. Each catch block should display description of exception along with the stack trace.

Write a main class to test your VirtualCafe. Inside main method, provide a console-based menu with following functionalities:
• Creating a virtual customer
• Reading a double type value from user representing temperature of coffee cup and constructing a coffee cup with that value
• Reading a double type value from user representing temperature of tea cup and constructing a tea cup with that value
• Serving a customer with a cup of coffee or tea as ordered
here is the code.
CoffeeCup

package Cafe;

public class CoffeeCup {
    double tempreture;
void setTempreture(double t){
    tempreture=t;
}
double getTempreture(){
    return tempreture;
}
CoffeeCup(double t){
    tempreture=t;
}
CoffeeCup(){
    tempreture=75;
}

}

TeaCup

package Cafe;


public class TeaCup {
double tempreture;
void setTemreture(double t){
    tempreture=t;
}
double getTemreture(){
    return tempreture;
}
TeaCup(double t){
    tempreture=t;
}
TeaCup(){
    tempreture=70;
}
}

and the problem code is here....toString() method is not overriding give error 'attempting to assign weaker access privileges;was public'.
TemperatureException

package java.Exception;
public class TemperatureException {
TemperatureException(){

}
TemperatureException(String ex){
    System.out.println(ex);
}
void toString(){
    TemperatureException e=new TemperatureException();
    System.out.print(e);
}
}

The toString() method has public access privileges which you can't change. If you want to override it, you need to make your method public as well. Also, the toString method returns a String, while yours returns void. Change your method name to: public String toString(). And return a String in the method.

commented: Helpful reply. +9

The toString() method has public access privileges which you can't change. If you want to override it, you need to make your method public as well. Also, the toString method returns a String, while yours returns void. Change your method name to: public String toString(). And return a String in the method.

First of Thanks for quick reply !
now I am faced with one more problem, now when i have created VirtualPerson class and VirtualCafe class. I am getting Incompatible types error !
VirtaulPerson:

package Cafe;


public class VirtualPerson {
public void drinkCoffee(CoffeeCup cup,double temp){
    if(temp>=65&&temp<85){
        throw new TooColdException("Cold!");
    }
    else if(temp>=85){
        throw new TooHotException("Hot!");
    }
}
}

and VirtualCafe:

package Cafe;
import java.Exception.TemperatureException.*;

public class VirtaulCafe {
static void serveCustomer(VirtualPerson customer,CoffeeCup cup,double temp){
   try{
       customer.drinkCoffee(cup, temp);
   }
   catch(TooColdException e){
       System.out.println(e.toString());
   }
}
}

Thanks in ADvance !

Don't you need to catch the TooHotException as well in your serveCustomer method?

Yes I have to Include TooHotException but I already getting error with TooColdException!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.