Question

Lab Objectives Be able to write methods Be able to call methods Be able to declare...

Lab Objectives

Be able to write methods

Be able to call methods

Be able to declare arrays

Be able to fill an array using a loop

Be able to access and process data in an array

Introduction

Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order.

This also allows for efficiencies, since the method can be called as many times as needed without rewriting the code each time.

In this project, we will also be introducing arrays. An array is an object that can store a group of values, all of the same type. Creating and using an array in Java is similar to creating and using any other type of object.

Task #1 void Methods

1. Create the file YournameProjectThreeTask1.java

2. In the main method, declare and create an array of doubles of size 5 called ftemp.

3. Add a line in the main method that calls the enterTemps method.

4. Write a static method that takes your array as its parameter and does not return a value. In this method, the user should be able to enter any five Fahrenheit temperatures.

Task #2 Value-Returning Methods

Write a static method called Celsius that takes in one element of your array at a time that represents the Fahrenheit temperature and returns the Celsius temperature using the formula

         C = 5/9 * (f – 32).

Task #3 Output

  In the main method, create a loop that displays the Fahrenheit and Celsius temperatures in the following table format.

   Fahrenheit Celsius

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

16 -8.89

15 -9.44

15 -9.44

16 -8.89

20 -6.67



Task #4 Sorted Array

   

1. You will need to write code that instantiates a new int array called mathArray.

2. After allocating an array of size 10 in the main method, you will need to write the following methods:

enterGrades — Use a for loop to repeatedly display a prompt for the user which should indicate that user should enter score number 1, score number 2, etc. Note: The computer starts counting with 0, but people start counting with 1, and your prompt should account for this. For example, when the user enters score number 1, it will be stored in indexed variable 0.

selectionSort — this method uses the selection sort algorithm to rearrange the data set from highest to lowest

calculateMean — this is a method that uses a for loop to access each score in the array and add it to a running total. The total divided by the number of scores (use the length of the array), and the result is stored into the mean and returned.

3. Display the array before and after the sort. You will also need to display the mean of the data set in an appropriate message.


Task #5 Arrays of Objects (Strings)

Create a file YournameCompactDisc.java. Declare an array of Strings, called cd, with a size of 6.

Fill the array by creating a new song with the title and artist and storing it in the appropriate position in the array. You need to read this information from the file Classics.txt that is provided. You will need to read the title and the artist and merge them together before storing them into the array. You will need to merge the word “by” in between the two strings.

Print the contents of the array to the console.

Compile, debug, and run. Your output should be as follows:

              

Contents of Compact Disc

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

Ode to Joy by Bach

The Sleeping Beauty by Tchaikovsky

Lullaby by Brahms

Canon by Bach

Symphony No. 5 by Beethoven

The Blue Danube Waltz by Strauss


   

Task # 6 2D Array operations

Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods:

• getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array.

• getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array.

• getRowTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the total of the values in the specified row.

• getColumnTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a column in the array. The method should return the total of the values in the specified column.

• getHighestInRow. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the highest value in the specified row of the array.

• getLowestInRow. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the lowest value in the specified row of the array.

Demonstrate each of the methods in this program.

Task # 7 2D ArrayLists

A supermarket wants to reward its best customer of each day, showing the customer’s name

on a screen in the supermarket. For that purpose, the customer’s purchase amount is stored in an ArrayList<Double> and the customer’s name is stored in a

corresponding ArrayList<String>. Implement a method

public static String nameOfBestCustomer(ArrayList<Double> sales,

   ArrayList<String> customers)

that returns the name of the customer with the largest sale. Write a program that prompts the cashier to enter all customer’s purchase amounts and names, adds them to two array lists, calls the method that you implemented, and displays the result. Use a price of 0 as a sentinel.


0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !!

Note:
As per Chegg strict policy & guidelines, I am only allowed to answer the first question its  subparts, please post the remaining questions as a separate post, and I will be happy to answer them. Sorry for the inconvenience caused.

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


import java.util.Scanner;

public class YournameProjectThreeTask1 {


    public static void main(String[] args) {

        //create an array of doubles of size 5 called ftemp.
        double ftemp[] = new double[5];

        //. Add a line in the main method that calls the enterTemps method.
        enterTemps(ftemp);

        // In the main method, create a loop that displays the Fahrenheit and Celsius temperatures in the following table format.

        System.out.printf("%-10s%10s\n", "Fahrenheit", "Celsius");
        System.out.println("====================");
        for (int i = 0; i < ftemp.length; i++) {
            System.out.printf("%-10.2f%10.2f\n", ftemp[i], Celsius(ftemp[i]));
        }
    }

    //Write a static method that takes your array
    // as its parameter and does not return a value.
    private static void enterTemps(double[] ftemp) {

        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < ftemp.length; i++) {
            System.out.print("Enter temperature (in Fahrenheit): ");
            ftemp[i] = scanner.nextDouble();
        }
    }

    //Write a static method called
    // Celsius that takes in one element of your
    // array at a time that represents the Fahrenheit
    // temperature and returns the Celsius temperature using the formula
    private static double Celsius(double fahrenheitTemp) {

        return (fahrenheitTemp - 32) * 5 / 9.0;
    }
}

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

Add a comment
Know the answer?
Add Answer to:
Lab Objectives Be able to write methods Be able to call methods Be able to declare...
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
  • Java Programming (use jGrasp if not thats fine) Write a Two classes one class that creates...

    Java Programming (use jGrasp if not thats fine) Write a Two classes one class that creates a two-dimensional 2 rows by 3 columns double array. This class will have a separate method for the user to populate the array with data values. The other class that has the following methods:   getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array.   getAverage. This method should accept a two-dimensional array...

  • Java Programming Assignment Write a class named 2DArrayOperations with the following static methods: getTotal . This...

    Java Programming Assignment Write a class named 2DArrayOperations with the following static methods: getTotal . This method should accept a two-dimensional array as its argument and return the total of all the values in the array. Write overloaded versions of this method that work with int , double , and long arrays. (A) getAverage . This method should accept a two-dimensional array as its argument and return the average of all the values in the array. Write overloaded versions of...

  • This is java, please follow my request and use netbeans. Thank you. A3 15. 2D Array...

    This is java, please follow my request and use netbeans. Thank you. A3 15. 2D Array Operations Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: • getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array. .getAverage. This method should accept a two-dimensional array as its argument and return...

  • Please solve only if you know how to do it. Write the code using C++ (not...

    Please solve only if you know how to do it. Write the code using C++ (not Python or Java). Show and explain everything neatly. COMMENTS (7.5% of programming assignment grade): Your program should have at least ten (10) different detailed comments explaining the different parts of your program. Each individual comment should be, at a minimum, a sentence explaining a particular part of your code. You should make each comment as detailed as necessary to fully explain your code. You...

  • Write a new program called TrickOr Treat that will do the following 1. In a while...

    Write a new program called TrickOr Treat that will do the following 1. In a while loop, say "Trick or Treat!" and allow the user to type in the name of a candy and store it in an ArrayList. Once the user types "Trick", then the loop should stop. Then, print out the total number of candies on the screen. (See example below) [1 points] 2. Write a method called countSnickers that will take an ArrayList of candy as an...

  • Develop a set of static methods in a class called Array Tools that perform the functions...

    Develop a set of static methods in a class called Array Tools that perform the functions below, and overload the methods for the specified types: Description Returns the minimum value stored in an array. Array Tools Method char minimum char array[]) int minimum( int array[] ) double minimum double arrayll ) char maximum char array() ) int maximum( int array[] ) double maximum( double array[] ) Returns the maximum value stored in an array. int minimumAt( char array()) int minimumAt(...

  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...

  • Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes...

    Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...

  • (JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students...

    (JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students identified by rows and test scores identified by columns. Ask the user for the number of students and for the number of tests (which will be the size of the two-dimensional array). Populate the array with user input (with numbers in {0, 100} for test scores). Assume that a score >= 60 is a pass for the below. Then, write methods for: Computing the...

  • Arrays In this homework, create two arrays – studentName and studentScore (String and Int types). Write...

    Arrays In this homework, create two arrays – studentName and studentScore (String and Int types). Write a method (getData) that would ask user to enter a set of data – for example, five student names and corresponding scores. This program should also have the following methods:     getSum. This method should accept a studentScore array as its argument and return the sum of the values in the array.     getAverage. This method should accept a studentScore array as its argument...

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