In: Computer Science
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];
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 :)