Question

Write a class encapsulating the concept of daily temperatures for a week with a single dimensiona...

Write a class encapsulating the concept of daily temperatures for a week with a single dimensional array of temperatures. Write the following methods: • A constructor accepting an array of seven temperatures as a parameter. • Accessor, mutator, toString() and equals() methods • A method returning how many temperatures were below freezing. • A method returning an array of temperatures above 100 degrees. • A method returning the largest change in temperature between any two consecutive days. • A method returning an array of daily temperatures, sorted in descending order (use one of the sorting algorithms in the textbook. Write a client class to test all the methods in your class.

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

Temperature.java

package package1;

import java.util.ArrayList;

import java.util.List;

public class Temperature {

double[] temperatures;

int NUMOFTEMP=7;

//Constructor

public Temperature(double[] temperatures){

this.temperatures=temperatures;

}

//Accessor and mutators

public double[] getTemperatures() {

return temperatures;

}

public void setTemperatures(double[] temperatures) {

this.temperatures = temperatures;

}

//toString method to print array

public String toString(){

String output="Daily temperatures for a week: \n";

for(int i=0;i<NUMOFTEMP;i++){

output+=temperatures[i]+" ";

}

return output;

}

//equals method to check if two arrays are equal

public boolean equals(Object o){

//Check passed object is an instance of Temperature class

if(o instanceof Temperature){

Temperature temp=(Temperature)o;

//Check if temperature array is equal to array of passed object. If yes, return true else false

if(temp.getTemperatures()==temperatures)

return true;

else

return false;

}

return false;

}

public int belowFreezing(){

int temperatureBelowFreezing=0;

//Iterate over the temperatures array

for(int i=0;i<NUMOFTEMP;i++){

//If temperature is less than 0, increment temperatureBelowFreezing

if(temperatures[i]<0){

temperatureBelowFreezing+=1;

}

}

//return temperatureBelowFreezing

return temperatureBelowFreezing;

}

public double[] tempAbove100(){

//List to store temperatures above 100

List<Double> temp=new ArrayList<Double>();

//Iterate over the temperatures array

for(int i=0;i<NUMOFTEMP;i++){

//If temperature is >100,add it to list

if(temperatures[i]>100){

temp.add(temperatures[i]);

}

}

//Create an array to store temperatures of list size

double[] temperatureAbove100=new double[temp.size()];

//Iterate over list and add temperature to new array

for(int i=0;i<temp.size();i++){

temperatureAbove100[i]=(double)temp.get(i);

}

//return array

return temperatureAbove100;

}

public double largestChange(){

//This variable represents largest change

double max=0;

//Iterate over the temperatures array

for(int i=0;i<NUMOFTEMP-1;i++){

//Check if difference between consecutive temperature > current max

if(temperatures[i+1]-temperatures[i]>max){

max=temperatures[i+1]-temperatures[i];

}

}

return max;

}

public double[] sortedTemp(){

//Used Bubble sort to sort temperature array in descending order

for(int i=0;i<NUMOFTEMP;i++){

for(int j=1;j<NUMOFTEMP-i;j++){

if(temperatures[j-1]<temperatures[j]){

double temp=temperatures[j-1];

temperatures[j-1]=temperatures[j];

temperatures[j]=temp;

}

}

}

//return sorted array

return temperatures;

}

}

TemperatureTester.java

package package1;

public class TemperatureTester {

public static void main(String[] args) {

double[] temperatures={23.4,25,101,-2,-4,104,67};

Temperature tempObj=new Temperature(temperatures);

//This will print all the temperatures

System.out.println(tempObj);

//Calculate temperature below freezing point

int belowFreezing=tempObj.belowFreezing();

System.out.println("Total temperatures below freezing point : "+belowFreezing);

// Temperature above 100 degrees

double[] temps=tempObj.tempAbove100();

System.out.println("Temperature above 100 degress : ");

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

System.out.print(temps[i]+" ");

}

//Calculate largest change

double largestChange=tempObj.largestChange();

System.out.println("\nLargest change in temperature between two consecutive days : "+largestChange);

//Sort temperatures

double[] sortedTemps=tempObj.sortedTemp();

System.out.println("Temperature in sorted manner: ");

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

System.out.print(sortedTemps[i]+" ");

}

//Check equality of two temperature objects

if(sortedTemps.equals(tempObj)){

System.out.println("\nTemperature arrays are equal");

}else{

System.out.println("\nTemperature arrays are not equal");

}

}

}

OUTPUT-

<terminated> TemperatureTester [Java Application] C:\Program FilesJava\jdk1.8.0_121 binljavaw.exe (C aily temperatures for a

Add a comment
Know the answer?
Add Answer to:
Write a class encapsulating the concept of daily temperatures for a week with a single dimensiona...
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
  • write a class encapsulating the concept of a student assuming that the student has the following...

    write a class encapsulating the concept of a student assuming that the student has the following attributes the name of student the average of the student the rite a class encapsulating the concept of a Student, assuming that the Student has the following attributes: the name of the student, the average of the student, and the student's GPA. Include a default constructor, an overloaded constructor, the accessors and mutators, and methods, toString() and equals(). Also include a method returning the...

  • You will need to first create an object class encapsulating a Trivia Game which INHERITS from...

    You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: description - which is a string write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money - double 3. number of questions that must be answered to win - integer. 4. write the accessor, mutator, constructor,...

  • Write a class Store which includes the attributes: store name, city. Write another class encapsulating an...

    Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method returning the...

  • In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now...

    In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now that you have successfully created Trivia objects, you will continue 6B by creating a linked list of trivia objects. Add the linked list code to the Trivia class. Your linked list code should include the following: a TriviaNode class with the attributes: 1. trivia game - Trivia object 2. next- TriviaNode 3. write the constructor, accessor, mutator and toString methods. A TriviaLinkedList Class which...

  • Write a class encapsulating the concept of a television set, assuming a television set has the...

    Write a class encapsulating the concept of a television set, assuming a television set has the following attributes: a brand and a price. include a constructor, the accessors and mutators, and methods to string and equals. Write a client class to test all the methods in your class.

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and...

    In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and linked lists. You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: 1. description - which is a string 2. write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money...

  • [CODE] Write a class encapsulating the concept of a house, assuming a house has the following...

    [CODE] Write a class encapsulating the concept of a house, assuming a house has the following attributes: value (in dollars), a city location, and number of bedrooms. Your class name should be “House” and your code should be in a file called “House.java”. You will need to create this file. In this class, include: Private instance variables for the previously-mentioned attributes A default constructor An overloaded constructor Accessor and mutator methods (i.e., get and set methods) The toString method The...

  • IN JAVA Write a class Store which includes the attributes: store name, city. Write another class...

    IN JAVA Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...

  • Write a class encapsulation the concept of coins (Coins java), assuming that coins have the following...

    Write a class encapsulation the concept of coins (Coins java), assuming that coins have the following attributes: a number of quarters, a number of dims, a number of nickels, and a number of pennies. Include a constructor (accept 4 numbers that represent the number of coins for each coin type), mutator and accessot methods, and method tostring. The tostring method should return a string in the following format: Total Value: $5.50 10 quarters, 20 dimes, 19 nickels, and 5 pennies...

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