• Register
1 vote
70 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

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,240 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
2 answers 104 views
104 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 maddi86 5.4k points
0 votes
1 answer 68 views
68 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 maddi86 5.4k points
0 votes
1 answer 44 views
44 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 samhaz 5k points
0 votes
1 answer 51 views
51 views
Problem: Can anyone help it was actually asked in exam as following and I need to know whether my answer was right or not? Write an expression that whose value is the fifth character of the String name?
asked Feb 24 maddi86 5.4k points
0 votes
1 answer 103 views
103 views
Problem: I am new in programming and stuck in a query, can anyone guide me? Write an expression that evaluates to True if the str associated with s starts with "p"?
asked Feb 17 maddi86 5.4k points