Question
THIS IS A C++ QUESTION! PLEASE ANSWER THE QUESTION CORRECTLY! Read the question thoroughly!

THIS QUESTION WANTS THE A USER INPUT!

SO FOR EXAMPLE THE CODE MUST MAKE IT SO THE USER HAS TO INPUT A TEMPERATURE. AND WHEN THE USER INPUTS A TEMP IT MUST OUTPUT IF THE TEMP ENTERED IS TO HIGH OR LOW.

Again please read the question carefully and READ the notes I left also!

Write a Thermostat class such that the user of the Thermostat class can create an object of Thermostat and set it to the desi
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
using namespace std;

class TemperatureOutofRange{
   public:
   int low,high;
   void setLow(int l){
       low=l;
   }
   void setHigh(int h){
       high=h;
   }
};

class TemperatureTooLow: public TemperatureOutofRange{
   public:
  
   void checkTemp(int l){
       if(l<low)
       throw "TemperatureTooLow Exception";
   }
  
};
class TemperatureTooHigh: public TemperatureOutofRange{
   public:
  
   void checkTemp(int h){
       if(h>high)
       throw "TemperatureTooHigh Exception";
   }
  
};

class Thermostat{
   public:
   int low,high;
   TemperatureTooHigh H1;
   TemperatureTooLow L1;
   Thermostat(int l,int h){
       low=l;
       high=h;
       H1.setLow(low);
       H1.setHigh(high);
       L1.setLow(low);
       L1.setHigh(high);
   }
   void setTemp(int temp){
       //check lower bound
       try{
           L1.checkTemp(temp);
       }
       catch(char const*excp ){
           cout<<excp<<endl;
       }
       //check upper bound
       try{
           H1.checkTemp(temp);
       }
       catch(char const*excp ){
           cout<<excp<<endl;
       }
   }
};

int main() {
   // your code goes here
   Thermostat t(0,100);
   t.setTemp(50);
   t.setTemp(150);
   t.setTemp(-50);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
THIS IS A C++ QUESTION! PLEASE ANSWER THE QUESTION CORRECTLY! Read the question thoroughly! THIS QUESTION WANTS THE A...
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
  • What is wrong with this code? Please bold the changes that were made so this code...

    What is wrong with this code? Please bold the changes that were made so this code runs properly. The Problem is: 11.16 (Catching Exceptions with SuperClasses) Use inheritance to create an exception superclass (called Exception) and exception subclasses ExceptionB and ExceptionC, where ExceptionB inherites from ExceptionA and ExceptionC inherits from ExeptionB. Write a program to demonstrate that the catch block for type ExceptionA catches exceptions of types ExceptionB and ExceptionC. I got this far but I'm still getting a lot...

  • Question 20 Statements that might generate an exception are placed in a catch block. True False...

    Question 20 Statements that might generate an exception are placed in a catch block. True False 1 points Question 21 The class Exception and its subclasses are designed to catch exceptions that should be caught and processed during program execution. True False 1 points Question 22 Which of the following statements is NOT true about creating your own exceptions? Typically, constructors are the only methods that you include when you define your own exception class. The exception class that you...

  • Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the...

    Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...

  • Output Enter base: 2 supply a list of digits separated by space: 1 0 0 1...

    Output Enter base: 2 supply a list of digits separated by space: 1 0 0 1 The value for Base = 2 and digits = 1 0 0 1 is 9 Enter base: 16 supply a list of digits separated by space: 99 All digits must be in the range [0,n) Enter base: 16 supply a list of digits separated by space: 9 9 The value for Base = 16 and digits = 9 9 is 153 Enter base: 2...

  • I need the following written in Java please, thank you ' We want you to implement...

    I need the following written in Java please, thank you ' We want you to implement a java class that will show details on users and throw exceptions where needed. The following requirements specify what fields you are expected to implement in your User class: - A private String firstName that specifies the first name of the user - A private String lastName that specifies the last name of the user - A private int age that specifies user's age...

  • PLEASE I NEED C# Objectives Learn the basics of exception handling. Background Exceptions are essentially unexpected...

    PLEASE I NEED C# Objectives Learn the basics of exception handling. Background Exceptions are essentially unexpected situations. It is difficult to write a program that can handle all possible situations, as we have found out through the many programs we have written. For example, should your program accept accidental input? Does it use the metric system or the empirical system? Do users of your program know which system it uses? In order to deal with all these possibilities, exceptions were...

  • absolute C++ QUESTION 1 Which statement is incorrect? When an event occurs that cannot be managed...

    absolute C++ QUESTION 1 Which statement is incorrect? When an event occurs that cannot be managed locally, an exception may be thrown. Throwing the exception object transfers control and information gleaned locally to some calling program unit that manages the event, or the program terminates If an exception is thrown in a function, sayf, but not handled there, the exception is propagated to the function that called fo In C++, an exception object can be a user-defined type or any...

  • please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1....

    please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1. Write a program for the following. NOTE that some of these steps are not dependent on each other. Using methods is mandatory. Make sure to use methods where it makes sense. a. Ask the user for a series of integers entered from the keyboard. Use a sentinel value such as 999 to end the input process. If the entered values are not integers, throw...

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

  • Please help me to answer the 5 programming questions and give some specific explanations, thanks a...

    Please help me to answer the 5 programming questions and give some specific explanations, thanks a lot !!! Question Consider a class hierarchy that includes a class called Creature, with subclasses called Unicorn and Dragon. The Creature class has a method called feelsLike, Not yet answered which is overridden in the Unicorn and Dragon class. Marked out of The feelsLike method of the Creature class returns "smooth", while the feelsLike method is overridden in the Unicorn class to return "fluffy"...

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