• Register
1 vote
109 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,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 249 views
249 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 97 views
97 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 90 views
90 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 25 views
25 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 5.5k points
0 votes
1 answer 67 views
67 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, 2020 maddi86 5.4k points
0 votes
1 answer 162 views
162 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, 2020 maddi86 5.4k points
0 votes
1 answer 34 views
34 views
Problem: I am making a program which consists of solving a 2x2 equation, for this I used the Cramer method, the problem is not how to solve it, but it throws an error when executing it, and I suspect that it is because I used methods of the Matrix class Inside the Vector class ... " using namespace std; int main () { cout << "As long as the program compiles well it is enough" << endl; return 0; }
asked Nov 5, 2020 sasha 5.3k points
1 vote
1 answer 30 views
30 views
Problem: I have got an assignment from my online java class and doing the assignment and can't quite figure out what I'm doing wrong. I have to write a program that finds the sum of all squares between 1 and 100. As part of the assignment, I'm required to use a "while" ... ); a++; sum = (sum + a); } System.out.println(sum); } Thanks for your positiveness and effort. I must recognise your help.
asked Mar 28, 2020 Gavin 15.3k points
0 votes
1 answer 19 views
19 views
I wonder if you could change the value of an external input variable that is treated as a function parameter, I remember seeing a way to do it but I don't remember very well. Suppose we use a function for the sum of two numbers, which has two parameters, the first ... of the sum =" << number << endl; The result of the sum should be stored in the variable. Would this procedure really work?
asked Oct 1, 2020 sasha 5.3k points
0 votes
1 answer 27 views
27 views
Problem: Which of the following is NOT a rule that must be followed when naming identifiers? A. Identifiers for constants should use an underscore between words. B. Identifiers should use periods to separate words. C. Identifiers should list the data type at the end of the name. D. Identifiers can contain spaces.
asked Mar 9, 2020 ArifulIslam 5.6k points