1.
import java.util.Scanner;
public class Test1{
public static void main(String []args){
Scanner input = new Scanner(System.in);
double[] student1Scores = new double[4];
System.out.println("How many students do you want to record?");
int noOfStudents = input.nextInt();
String[] names = createNames(noOfStudents);
System.out.println("Please enter their scores:");
for(int i=0;i<names.length;i++)
{
System.out.println("Enter score for " + names[i]);
for(int j=0;j<student1Scores.length;j++)
{
student1Scores[j] = input.nextDouble();
}
double average = averageScore(student1Scores);
System.out.println(names[i] + " average score is " + average);
char grade = determineGrade(average);
System.out.println(names[i] + " grade is then " + grade);
}
System.out.println("Displaying all names!");
displayNames(names);
}
public static String[] createNames(int noOfStudents)
{
String[] n = new String[noOfStudents];
Scanner input = new Scanner(System.in);
System.out.println("Please enter " + noOfStudents + " student's names");
for(int i=0;i<n.length;i++)
{
n[i] = input.nextLine();
}
System.out.println("You have entered all names!TQ");
return n;
}
public static void displayNames(String[] names)
{
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
public static char determineGrade(double average)
{
if(average > 90)
return 'A';
else if(average > 80)
return 'B';
else return 'C';
}
public static double averageScore(double[] x)
{
double sum = 0;
for(int i=0; i<x.length;i++)
{
sum = sum + x[i];
}
double average = sum/x.length;
return average;
}
}
2.
import java.util.Scanner;
public class Test2{
public static void main (String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter number of rows and column of array:");
int rows = input.nextInt();
int columns=input.nextInt();
double [][]table=new double[rows][columns];
System.out.println("Enter array:");
for(int i=0;i<table.length;i++)
{
for(int j=0;j<table[i].length;j++)
{
table[i][j]=input.nextDouble();
}
}
int[] location=locateLargest(table);
System.out.printf("The location of the largest element is (%d,%d)%n",location[0],location[1]);
}
public static int [] locateLargest(double[][]a)
{
int [] location=new int [] {0,0};
double largest=a[0][0];
for (int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
if(largest<a[i][j])
{
largest=a[i][j];
location[0]=i;
location[1]=j;
}
}
}
return location;
}
}
3.
import java.util.Scanner;
public class Test3{
public static void main(String []args){
final int Total_numbers=6;
int[] numbers = new int[Total_numbers];
Scanner input=new Scanner (System.in);
System.out.println("Enter" + number.length + "integer numbers: ");
//Read all numbers
for (int i=0;i<numbers.length;i++)
{
number[i]=input.nextInt();
}
//find the largest element
int max=numbers[0];
for (int i=1;i<numbers.length;i++)
{
if (numbers[i]>max)
{
max=numbers[i];
}
}
//find the smallest element
int min=number[0];
for (int j=1;j<numbers.length;j++)
{
if (numbers[j]<min)
{
min=numbers[j];
}
}
//find the occurence of the largest number
int count1=0;
for(inti=0; i<numbers.length;i++)
{
if(numbers[i]==max)
count1++;
}
//find the occurence of the smallest number
int count2=0;
for(int j=0;j<numbers.length;j++)
{
if(numbers[j]==min)
count2++;
}
int swap=numbers[0];
for(int i=0; i<numbers.length/2;i++)
{
int temp=numbers[i];
numbers[i]=numbers[numbers.length-i-1];
numbers[numbers.length-i-1]=temp;
}
//prepare the result
System.out.println("Output:");
System.out.println("The array is:");
for (int i=0; i<numbers.length;i++)
{
System.out.println(numbers[i] + "");
System.out.println();
System.out.println("The largest number is " + max);
System.out.println("The smallest number is" + min);
System.out.println("The occurrence count of the largest number is" + count1);
System.out.println("The occurence count of the smallest number is" + count2);
}
}
}
}
4.
import java.util.Scanner;
public class Test4 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
final int maxrange=10;
final int minrange =-1;
final int range=10;
int[] list=new int[maxrange];
for(int i=0; i<list.length;i++)
{
list[i]=0;
}
System.out.println("Enter a list of integer between 1 and 100");
System.out.println(" To stop, enter an integer not in this range");
System.out.println("Enter integer");
int value= input.nextInt();
while (value>=minrange && value<=(maxrange*range))
{
list[(value-1)/range]=list[(value-1)/range]+1;
System.out.println("Enter integer");
value=input.nextInt();
}
System.out.println("Here is your histogram");
for (int i=0;i<list.length;i++)
{
System.out.println(" " + (i*range+1) + " " + (i+1)*range+ "\t|");
for (int j=0; j<list[i];j++)
{
System.out.println("*");
}
System.out.println();
}
}
}