Question

In: Computer Science

Write the appropriate method headings using the provided method name for the following processes (2 pts...

Write the appropriate method headings using the provided method name for the following processes (2 pts each).

1. Calculate and return the sum of two decimal numbers - getSum.

2. Compute and return the average speed of a car, given the distance traveled (as type double) and traveling time (in hours and minutes, both of type int) - getAverageSpeed.

3. Given the radius of a circle, print the area of a circle - printArea.

4. Given a student’s name and three test scores (of type int), print the student’s name and average test score - printStudentDetails

A. Array Declarations(2 pts each). Determine whether the following array declarations are valid. If a declaration is valid, write VALID. If it is invalid, you can make assumptions and write a correct declaration

1. int size;

double[] list = new double[size];

2. int[] test = new int[-10];

3. double[] sales = new double[40.5];

4. int x, y;

har[][] board = new board[x][y];

Solutions

Expert Solution

The code for questions 1 to 4 is :


import java.util.Scanner;

public class Functions {
        
        //Question (1) getSum() function
        //This function calculates two decimal numbers and prints result
        public static void getSum() {
                float a1;
                float a2;
                //Creating a object scnr of class Scanner() to read input from user.
                Scanner scnr = new Scanner(System.in);
                System.out.printf("Enter first decimal value :");
                //Scanner object.nextInt() reads a integer value from user
                //For float numbers .nextFloat()
                //For strings nextLine()
                a1 = scnr.nextFloat();
                System.out.printf("Enter second decimal value : ");
                a2=scnr.nextFloat();
                float result=a1+a2;
                
                System.out.printf("Sum of two decimal numbers is %.2f+%.2f= %.2f\n",a1,a2,result);
                
                
        }
        
        //Question (2) getAverageSpeed()
        //This function takes distance,time as inputs and prints average speed
        //I have taken meter/second as units for speed
        public static void getAverageSpeed() {
                double distance;
                int hours;
                int minutes;
                Scanner scnr1 = new Scanner(System.in);
                System.out.printf("\nEnter distance travelled in km :");
                distance=scnr1.nextDouble();
                System.out.printf("Enter No.of hours travelled : ");
                hours =scnr1.nextInt();
                System.out.printf("Enter mins :");
                minutes = scnr1.nextInt();
                System.out.printf("The average speed of the car is: %.3f m/sec\n",(distance*1000)/((hours*60+minutes)*60));
                
        }
        
        //Question (3) printArea()
        //This function takes radius as input and prints area of circle
        public static void printArea() {
                Scanner scnr1 = new Scanner(System.in);
                System.out.printf("\nEnter radius of circle: ");
                float radius=scnr1.nextFloat();
                System.out.printf("Area of circle with radius %f is %f\n",radius,(3.14*radius*radius));
                
        }
        
        //Question (4) printStudentDetails()
        public static void printStudentDetails() {
                Scanner scnr1 = new Scanner(System.in);
                System.out.printf("\nEnter student name: ");
                String name =scnr1.nextLine();
                System.out.printf("Enter score 1: ");
                int score1=scnr1.nextInt();
                System.out.printf("Enter score 2: ");
                int score2=scnr1.nextInt();
                System.out.printf("Enter score 3: ");
                int score3=scnr1.nextInt();
                float avg= (score1+score2+score3)/3;
                System.out.printf("Average score of student %s in 3 tests is: %.2f",name,avg);
                
        }
        
        //Program execution starts from main() function
        //Every other function is executed only when it is called by main() function
        public static void main(String[] args) {
                //Calling functions one by one
                getSum();
                getAverageSpeed();
                printArea();
                printStudentDetails();
                }

}

I have included all 4 methods in same class, However if you need every method ( function ) seperately, please do let me know in the comment section and I will provide code for individual functions.

Question (A)

1) int size;

double [] list = new double[size]

This declaration is Invalid. Since the size of double list[] is given as size which is a variable of type int with no value in it. Which means you trying to do this : double [] list =new double [NULL] which is wrong declaration

The Valid declaration for this is :

int size=10;

double [ ] list = new double [size];

2) int [ ] test = new int [-10];

This declaration is Invalid. We can not declare a array with negative size.

correct declaration is int [ ] test = new int [10];

3) double [ ] sales = new double [40.5];

This declaration is Invalid. Type of the array is double, that doest mean size has to be a doube value. It just mean the elements stored in that array will be of double data type. Size of the array should alway be of type "int".

Correct declaration : double [ ] sales = new double [40];

4) int x,y;

har[ ] [ ] board = new board [x][y];

This declaration is invalid. Because the syntax for declaring an array is data_type [ ] array_name = new data_type [size] . But the above declaration has no data type mentioned on valid size.

The correct declaration is:

int x=3;

int y=3;

int [ ] [ ] board = new int [x][y];

Hope this answer helps you.

Thank you :)


Related Solutions

Using the provided code (found here), write a program using the main method where the user...
Using the provided code (found here), write a program using the main method where the user enters Strings and the program echoes these strings to the console until the user enters “quit”. When user quits the program should print out, “Goodbye”. You may assume that the case is ignored when the user enters, “quit”, so “quit”, “QUIT”, “Quit”,“qUiT”, etc. are all acceptable ways to end the program. The results should be printed out to the console in the following format:...
Using C#: Write a class named Employee that has the following properties: Name - The Name...
Using C#: Write a class named Employee that has the following properties: Name - The Name property holds the employee's name IdNumber - The IdNumber property holds the employee's ID number Department - The Department property holds the name of the department in which the employee works Position - The Position property holds the employee's job title The class should have the following overloaded constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate...
1. For each item, write what is required using only English words. (a) (2 Pts.) The...
1. For each item, write what is required using only English words. (a) (2 Pts.) The converse, contrapositive, inverse and negation of \If George feels well, then George is is going to a movie or going dancing". (b) (2 Pts.) The converse, contrapositive, inverse and negation of \Anna is failing history and psychology, then Anna is not graduating". (c) (2 Pts.) The statement represented by the symbols below and the negation of such statement: 8s9c (M(s) ! (D(c) ^ T(s;...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following method: • public String getName() Gets the name string. • public int consonants()...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following methods. • public String getName() Gets the name string. • public int consonants()...
. (30 pts) Write an appropriate hypothesis testing problem that’s related to your article for the...
. (30 pts) Write an appropriate hypothesis testing problem that’s related to your article for the population small sample mean.. You are free to choose a one‐sided or a two‐sided alternative. (10 pts) a. State the null and alternative hypotheses. (5 pts) b. Compute the test statistic under the assumption that the null hypothesis is true. Use two decimal places for ? scores and three decimal places for ? and ? values. (5 pts) c. Compute the ?‐?????. (5 pts)...
Direct Write-Off Method Journalize the following transactions, using the direct write-off method of accounting for uncollectible...
Direct Write-Off Method Journalize the following transactions, using the direct write-off method of accounting for uncollectible receivables: Oct. 2: Received $2,250 from Matthew Chapman and wrote off the remainder owed of $2,050 as uncollectible. If an amount box does not require an entry, leave it blank. Oct. 2 Dec. 20: Reinstated the account of Matthew Chapman and received $2,050 cash in full payment. Dec. 20-Reinstate Dec. 20-Collection Allowance Method Journalize the following transactions, using the allowance method of accounting for...
Direct Write-Off Method Journalize the following transactions, using the direct write-off method of accounting for uncollectible...
Direct Write-Off Method Journalize the following transactions, using the direct write-off method of accounting for uncollectible receivables: Oct. 2: Received $2,450 from William Pruitt and wrote off the remainder owed of $2,330 as uncollectible. If an amount box does not require an entry, leave it blank. Oct. 2 Dec. 20: Reinstated the account of William Pruitt and received $2,330 cash in full payment. Dec. 20-Reinstate Dec. 20-Collection 2.) Allowance Method Journalize the following transactions, using the allowance method of accounting...
Direct Write-Off Method Journalize the following transactions, using the direct write-off method of accounting for uncollectible...
Direct Write-Off Method Journalize the following transactions, using the direct write-off method of accounting for uncollectible receivables. Oct. 2: Received $2,980 from Paula Spitler and wrote off the remainder owed of $3,550 as uncollectible. If an amount box does not require an entry, leave it blank. Oct. 2 Dec. 20: Reinstated the account of Paula Spitler and received $3,550 cash in full payment. Reinstate Collection
write code to display "Hello terminal." On the terminal device with a name /dev/pts/1.
write code to display "Hello terminal." On the terminal device with a name /dev/pts/1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT