Programmers learn & share
+1 vote
27 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?

by (12.9k points)  
edited by | 27 views

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.

by (55.5k 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

 

 

by (10k points)  
2,221 questions
2,695 answers
59 comments
241 users