Question

I don't understand step 3 also can someone check if I did the setTemps method right?...

I don't understand step 3 also can someone check if I did the setTemps method right?

Step 2: Temps Class For lab9 we will be using arrays to store the high temperatures for a given month and perform some simple statistics for them. You will create a Temps class to store the values and perform the calculations. (Data that you use for this lab was obtained from the national weather service and city‐data.com – from June 2004)

Create a new class called Temps. This class has the following instance variables:

  • a String named monthName
  • an int named year
  • an array of ints called temps.

Write the following methods:

  • public Temps(String monthName, int year)- This constructor sets the year and the name of the month to the values of the input parameters. You will not do anything to temps in the constructor.
  • public void setTemps (String tempStr) to set the values into the array temps using the string parameter.
    • Use the split method to break apart the parameter into an array. Notice that the string contains several temperature values separated by commas.

Example: String [] aux = tempStr.split(",");

  • Then instantiate the array temps (why here and now?)
  • Use a loop to place the values of the aux array created above into the array temps.

Example: Converting the first entry of the aux array into an int and assigning the value to the first position of temps.

temps [0] = Integer.parseInt(aux[0]);

  • public double getAverage() - calculate and return the average temperature for all the values currently in temps.
  • public int getHighest() - find and return the highest value in temps.
  • public int getLowest() - find and return the lowest value in temps.
  • public String toString() - return a formatted string with the stats like the sample results below.

The average high temperature for June, 2004 was: 75.37

The high temperature for June, 2004 was: 90

The lowest high temperature for June, 2004 was: 63

  • public void printTemps() – prints all the temperatures – 1 line per each day – see sample output

Daily Temperatures

==================

Day 1: 73ºF

Day 2: 68ºF

Day 3: 74ºF

.

.

.

Day 30: 76ºF

Step 3: Create the main method

The main method could be included in the Temps class or you could create another class – the driver class - if you prefer that.

  1. Declare and instantiate a Temps type object (give it the name temperatures) pass the parameters "June" and 2004.

  1. Use the object created in the previous step to invoke the setTemps method, passing the following string as parameter.

"73,68,74,74,77,81,85,90,85,72,63,74,83,80,81,81,79,79,69,71,69,70,75,70,68,70,73,72,79,76"

  1. Invoke the method printTemps.

  1. Print out the object using the toString method.

public class Temps {
private String monthName;
private int year;
private int [] temps;
private int highest;
private int lowest;
private int total;
private String output;
private String [] aux;

public Temps () {
monthName = "June";
year = 2004;
highest = 0;
lowest = 0;
total = 0;
}

public Temps (String monthName, int year) {
this.monthName = monthName;
this.year = year;

}

public void setTemps (String tempStr) {
temps = new int [31];
String [] aux = tempStr.split(",");
for(int i = 0; i < temps.length; i++) {
temps [i] = Integer.parseInt(aux[0]);
}
}

public int getAverage() {
for(int i = 0; i < temps.length; i++){
total = total + temps[i];
}
return total / temps.length;
}

public int getHighest() {
int highest = temps[0];
for (int i = 1; i < temps.length; i++) {
if (temps[i] > highest) {
highest = temps[i];
}
}
return highest;
}

public int getlowest() {
int lowest = temps[0];
for (int i = 1; i < temps.length; i++) {
if (temps[i] < lowest) {
lowest = temps[i];
}
}
return lowest;
}

public String toString() {
for (int i = 1; i < temps.length; i++) {
String output = String.format("The average high temperature for " + monthName + "," + year + "was: " + (total / temps.length) +
"\n" + "The high temperature for " + monthName + "," + year + "was: " + highest +
"\n" + "The lowest high temperature for " + monthName + "," + year + "was: " + lowest);
}
return output;
}

public void printTemps() {
for (int i = 0; i < temps.length; i++) {
System.out.println("Daily Temperatures ");
System.out.println("================== ");
System.out.println("Day " + (i + 1) + ": " + temps + " F");

}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Step 3 is asking you to create a driver class which is a class that contains the main() method and demonstrates the methods in the other class (Temps). I created a class named "Main" which follows the instruction in step 3. There were tiny logical errors in the printTemps() and setTemps(), so i corrected those. however, I haven't checked rest of the methods

# If you have a query/issue with respect to the answer, please drop a comment. I will surely try to address your query ASAP and resolve the issue

# # Please consider providing a thumbs up to this question if it helps you. by doing that, you will help other students who are facing a similar issue.

//-------------------OUTPUT------------------------------------------

//-----------------------------------------------------------------------

public class Main {

    public static void main(String[] args) {

        Temps t=new Temps("June", 2004);

        t.setTemps("73,68,74,74,77,81,85,90,85,72,63,74,83,80,81,81,79,79,69,71,69,70,75,70,68,70,73,72,79,76");

        t.printTemps();

    }

}

class Temps {

    private String monthName;

    private int year;

    private int[] temps;

    private int highest;

    private int lowest;

    private int total;

    private String output;

    private String[] aux;

    public Temps() {

        monthName = "June";

        year = 2004;

        highest = 0;

        lowest = 0;

        total = 0;

    }

    public Temps(String monthName, int year) {

        this.monthName = monthName;

        this.year = year;

    }

    public void setTemps(String tempStr) {

        

        String[] aux = tempStr.split(",");

        temps = new int[aux.length];

        for (int i = 0; i < aux.length; i++) {

            temps[i] = Integer.parseInt(aux[i]);

        }

    }

    public int getAverage() {

        for (int i = 0; i < temps.length; i++) {

            total = total + temps[i];

        }

        return total / temps.length;

    }

    public int getHighest() {

        int highest = temps[0];

        for (int i = 1; i < temps.length; i++) {

            if (temps[i] > highest) {

                highest = temps[i];

            }

        }

        return highest;

    }

    public int getlowest() {

        int lowest = temps[0];

        for (int i = 1; i < temps.length; i++) {

            if (temps[i] < lowest) {

                lowest = temps[i];

            }

        }

        return lowest;

    }

    public String toString() {

        for (int i = 1; i < temps.length; i++) {

            String output = String.format("The average high temperature for " + monthName + "," + year + "was: "

                    + (total / temps.length) + "\n" + "The high temperature for " + monthName + "," + year + "was: "

                    + highest + "\n" + "The lowest high temperature for " + monthName + "," + year + "was: " + lowest);

        }

        return output;

    }

    public void printTemps() {

        System.out.println("Daily Temperatures ");

        System.out.println("================== ");

        for (int i = 0; i < temps.length; i++) {

            

            System.out.println("Day " + (i + 1) + ": " + temps[i] + " F");

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
I don't understand step 3 also can someone check if I did the setTemps method right?...
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
  • USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

    USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • Please follow the instructions carefully. Thank you! For the second activity, I outputted the superhero class...

    Please follow the instructions carefully. Thank you! For the second activity, I outputted the superhero class below. SUPERHERO CLASS public class Superhero{   private String alias;   private String superpower;   private int health;   public Superhero(){       alias= "unkown";       superpower= "unknown";       health= 50; //Realized I did not use default constructor while going through instructions of lab   }   public Superhero(String alias1, String superpower1,int health1 ){       alias=alias1;       superpower=superpower1;       if(health1>=0 && health1<=50)           health= health1;       else if(health1<0||health1>50)           health=25;   }   public void setalias(String alias1){       alias=alias1;   }   public void setsuperpower(String...

  • I need help with this one method in java. Here are the guidelines. Only public Employee[]...

    I need help with this one method in java. Here are the guidelines. Only public Employee[] findAllBySubstring(String find). EmployeeManager EmployeeManager - employees : Employee[] - employeeMax : final int = 10 -currentEmployees : int <<constructor>> EmployeeManager + addEmployee( type : int, fn : String, ln : String, m : char, g : char, en : int, ft : boolean, amount : double) + removeEmployee( index : int) + listAll() + listHourly() + listSalary() + listCommision() + resetWeek() + calculatePayout() :...

  • In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate...

    In the Employee class, add an implementation for the calculatePension() method declared in the interface. Calculate the pension to date as the equivalent of a monthly salary for every year in service. 4) Print the calculated pension for all employees. ************** I added the interface and I implemented the interface with Employee class but, I don't know how can i call the method in main class ************ import java.io.*; public class ManagerTest { public static void main(String[] args) { InputStreamReader...

  • I have completed a large portion of this work... but I don't know if it is...

    I have completed a large portion of this work... but I don't know if it is good enough. Please look it over and give me any advice, tips, critiques, etc. I think I may need more constructors. Here are the directions to this portion: Before you begin modifying and creating classes, your team lead reminds you to demonstrate industry standard best practices in all your code to ensure clarity, consistency, and efficiency among all software developers working on the program....

  • please help with program,these are he requirements: I am also getting a "missing return statement" in...

    please help with program,these are he requirements: I am also getting a "missing return statement" in my milestone class Create a new class Milestone which contains at least an event description, a planned completion date, and an actual completion date (which will be unset when the milestone is created). There should be a method to changed the planned completion date as well as a method to designate the Milestone as achieved. Add a method that returns whether the milestone has...

  • Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...

    Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then generate their documentation. Examine the documentation to see the logic used in creating each class. (Code given below) import java.util.ArrayList; import java.util.Iterator; class Calendar { private Year year; public Calendar() { year = new Year(); } public void printCalendar() { year.printCalendar(); } } class Year { private ArrayList<Month> months; private int number; public Year() { this(2013); } public Year(int number) { this.number = number;...

  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

  • Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular a...

    Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular array implementation of the bounded queue by growing the elements array when the queue is full. Add assertions to check all preconditions of the methods of the bounded queue implementation. My code is! public class MessageQueue{ public MessageQueue(int capacity){ elements = new Message[capacity];...

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