• Register
1 vote
191 views

Problem:

Hello fellas,

I was trying to solve problems from various websites. All the problems are the java-based problem. I’ve got a problem that says: Write the definition of a class weatherforecast that provides the following behavior (methods):

  1. A method called setSkies that has one parameter, a String.

  2. A method called setHigh that has one parameter, an int.

  3. A method called setLow that has one parameter, an int

So, the challenge is, I can only have those methods in my program. Do you any clue?

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution:

As you got the challenge of having a limit of methods. Let’s give it a try if we can solve the program by given methods: 

public class WeatherForecast{
   private String skies = "";
   private int high = 0;
   private int low = 0;
   public void setSkies(String theSkies){
      skies = theSkies;
   }
 
   public void setHigh(int theHigh){
      high = theHigh;
   }
    
   public void setLow(int theLow){
      low = theLow;
   }
 
   public String getSkies(){
      return skies;
   }
 
   public int getHigh(){
      return high;
   }
 
   public int getLow(){
      return low;
   }
}

I hope this helps you.

13 9 6
94,260 points
0 votes

Solution:

It seems as though you are missing some instance variables for you class.

Explanation:

The spec requires you have one String for describing the skies (ie cloudy, sunny) and two integers that represent the low and high temperatures for the day (ie low of 23 and high of 45) 

the spec says you do not have to deal with them in a constructor, yet you may if you wish.

Variables:

  1. string for the skies

  2. int for the low temp

  3. int for the high temp

Functions:

  1. get/set for skies. get should return self.skies, set should set the instance variable for the skies.

  2. get/set for low temp. get should return self.low, set should set the instance variable for the low temp.

  3. get/set for high temp. get should return self.high, set should set the instance variable for the high temp

 

Follow this below code:

class WeatherForecast():
    skies = "Clear"
    high = 80
    low = 20 
    def set_skies(self, skies):
        self.skies = skies
    def get_skies(self):
        return self.skies
    def set_high(self, high):
        self.high = high
    def get_high(self):
        return self.high
    def set_low(self, value):
        self.low = value
    def get_low(self):
        return self.low

 

Enter it just like this for myProgrammingLab. (Include the 'class WeatherForecast:" part.

class WeatherForecast:

    def __init__(self):
    self.skies=""
    self.low=0
    self.high=0

def set_skies(self,skies):
    self.skies = skies

def set_high(self, high):
    self.high = high

def set_low(self, low):
    self.low = low

def get_skies(self):
    return self.skies

def get_high(self):
    return self.high

def get_low(self):
    return self.low

 

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 7 views
7 views
Problem: I need a help about writing given definition, i am little bit confused about how to write, can anyone help me out?
asked Mar 26 niyatiry 3.9k points
0 votes
1 answer 17 views
17 views
Problem: How to fix this error: write the definition of a class weatherforecast that provides the following behavior (methods): HELP! I need help understanding this error: write the definition of a class weatherforecast that provides the following behavior (methods): Any ideas on what I need to do to fix this? Thanks!
asked Feb 28 Mashhoodch 13k points
0 votes
2 answers 542 views
542 views
Problem: I am new to programming, so need query to get query to return max number from 3 integer? Write the definition of a method max that has three int parameters and returns the largest? Can anyone guide with correct code?
asked Feb 17, 2020 maddi86 5.4k points
0 votes
1 answer 5 views
5 views
Problem: When I run the code, everything appears to work, but the numbers that I get in the printout are not accurate. Not sure if it is the constructor I have that is set up wrong or what. I've just been banging my head against the wall trying to figure it out. Any help/advice would be much appreciated!
asked Apr 13 ummesalma 29.2k points
0 votes
1 answer 219 views
219 views
Solutions: I am new in programming and my logic is not good, can anyone give the right solution? Write an expression that evaluates to true if and only if the C-string s equals the C-string "end"?
asked Feb 14, 2020 maddi86 5.4k points
0 votes
1 answer 10 views
10 views
Problem: I need a full definition for a class named counter and and containing other members. anyone here who can help me about this? Then please answer me.
asked Mar 26 niyatiry 3.9k points
0 votes
1 answer 3 views
3 views
Problem: I am in trouble while solving it .. please help me .. Write the definition of a class clock
asked Apr 27 Ifra 37.2k points
1 vote
1 answer 10 views
10 views
Problem: I’ve got a project in which I have to create a project which can take 3 integers and return the smallest one . You muse use Math class min function .
asked Apr 24 Raj Tyagi 15.6k points
0 votes
1 answer 186 views
186 views
I start programming recently and I love to do programming but every day I'm facing new problems. I wrote a java code where I can easily take two int input from a user and I'm able to print the smallest value. But I'm doing all things in the main method. I don't want to do ... smallest; if(a>b){ smallest = b; }else{ smallest = a; } System.out.println("Smallest is: "+smallest); } Here is my code.
asked May 3, 2020 samhaz 5k points
0 votes
1 answer 108 views
108 views
Problem: Hi there! I am new to the world of programming. My programming teacher gave me an assignment in which I have to write a program in which there would be a function that will take two strings as input from user and will compare the size of both strings ... the larger string of the two. I have no clue how to do that. Please provide me with the sample program with proper description. Thanks
asked Nov 25, 2020 Code Learner 9.9k points