1. Write a Java application that asks the user how many numbers wants stored in
ID: 3571833 • Letter: 1
Question
1. Write a Java application that asks the user how many numbers wants stored in a one-dimensional array. The program should allow the user to enter the numbers and calculate the total, average, highest and lowest of all those numbers.
2. Write a Java application that stores the names of your family and friends in a one-dimensional array of Strings. The program should show all names in upper case and lower case, identify the first character of the name, and the lengths of the names.
PLease do both program separately
Explanation / Answer
Question 1:
ArrayTest.java
import java.util.Scanner;
public class ArrayTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the size of an array: ");
int n = scan.nextInt();
int a[] = new int[n];
for(int i=0; i<n; i++){
System.out.print("Enter the element: ");
a[i] = scan.nextInt();
}
int total =0;
double average = 0;
int min =a[0], max = a[0];
for(int i=0; i<a.length; i++){
total = total + a[i];
if(min > a[i]){
min = a[i];
}
if(max < a[i]){
max = a[i];
}
}
average = total/(double)a.length;
System.out.println("Total is "+total);
System.out.println("Average is "+average);
System.out.println("Min is "+min);
System.out.println("Max is "+max);
}
}
Output:
Enter the size of an array: 5
Enter the element: 2
3Enter the element:
Enter the element: 1
Enter the element: 5
Enter the element: 4
Total is 15
Average is 3.0
Min is 1
Max is 5
Question 2:
StringNames.java
import java.util.Scanner;
public class StringNames {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the size of an array: ");
int n = scan.nextInt();
scan.nextLine();
String names[] = new String[n];
for(int i=0; i<n; i++){
System.out.print("Enter the name: ");
names[i] = scan.nextLine();
}
for(int i=0; i<n; i++){
System.out.println("Name: "+names[i]+" First charactr: "+names[i].charAt(0)+" Length of name: "+names[i].length());
}
}
}
Output:
Enter the size of an array: 5
Enter the name: Suresh
Enter the name: Sekhar
Enter the name: Anshu
Enter the name: murapaka
Enter the name: Revathi
Name: Suresh First charactr: S Length of name: 6
Name: Sekhar First charactr: S Length of name: 6
Name: Anshu First charactr: A Length of name: 5
Name: murapaka First charactr: m Length of name: 8
Name: Revathi First charactr: R Length of name: 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.