In: Computer Science
Write a Java programs.
Q.1. A freshman has 0-29 credits, a sophamore has 30-59, a junior has 60-89 and a senior has 90+ credits. Write a program to open the file and read all the contents. Report back to the user the number of freshmen, sophomores, juniors and seniors. Output the first and last names of the person with the highest GPA for each of those categories.
Q.2. (Occurrence of each letter) Write a Program that prompts the user to enter a file name and displays the occurrence of each letter in the file. Letters are case insensitive.
Students.java
The file students.txt has the following format on each line
fname(string) lname(string) credits(int) GPA(float) with a single space between each field.
For example
Mitchell Beck 11 2.88
Thelma Colon 43 2.25
Erma Mullins 68 1.98
Pedro Mack 17 1.95
.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// studentsData.java
Mitchell Beck 11 2.88
Thelma Colon 43 2.25
Erma Mullins 68 1.98
Pedro Mack 17 1.95
______________________
// StudentTypeCount.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class StudentTypeCount {
public static void main(String[] args) {
//Declaring variables
int
cntFreshman=0,cntSophamore=0,cntJunior=0,cntSenior=0;
double
maxFGPA=-999,maxSGPA=-999,maxJGPA=-999,maxSNGPA=-999;
String line,fname,lname;
String mFresh =
null,msopho=null,mjuni=null,mseni=null;
double gpa;
int credits;
try {
//Opening the
input file
Scanner sc=new
Scanner(new File("studentsData.txt"));
//Reading the
data from the file
while(sc.hasNext())
{
line=sc.nextLine();
String
array[]=line.split(" ");
fname=array[0];
lname=array[1];
credits=Integer.parseInt(array[2]);
gpa=Double.parseDouble(array[3]);
if(credits>=0
&& credits<=29)
{
//counting no of freshman
cntFreshman++;
//Finding the freshman who got the highest
gpa
if(maxFGPA<gpa)
{
maxFGPA=gpa;
mFresh=fname+" "+lname;
}
}
else
if(credits>=30 && credits<=59)
{
//counting no of sophomore
cntSophamore++;
//Finding the sophomore who got the highest
gpa
if(maxSGPA<gpa)
{
maxSGPA=gpa;
msopho=fname+" "+lname;
}
}
else
if(credits>=60 && credits<=89)
{
//counting no of junior
cntJunior++;
//Finding the junior who got the highest
gpa
if(maxJGPA<gpa)
{
maxJGPA=gpa;
mjuni=fname+" "+lname;
}
}
else
if(credits>=90)
{
//counting no of senior
cntSenior++;
//Finding the senior who got the highest
gpa
if(maxSNGPA<gpa)
{
maxSNGPA=gpa;
mseni=fname+" "+lname;
}
}
}
//Displaying the
count of students
System.out.println("No of Freshman :"+cntFreshman);
System.out.println("No of Sophomore :"+cntSophamore);
System.out.println("No of Junior :"+cntJunior);
System.out.println("No of Senior :"+cntSenior);
if(cntFreshman>0)
{
System.out.println("Among Freshman "+mFresh+"
got the highest gpa");
}
if(cntSophamore>0)
{
System.out.println("Among Sophomore "+msopho+"
got the highest gpa");
}
if(cntJunior>0)
{
System.out.println("Among Junior "+mjuni+" got
the highest gpa");
}
if(cntSenior>0)
{
System.out.println("Among Senior "+mseni+" got
the highest gpa");
}
} catch (FileNotFoundException e)
{
System.out.println("File Not Found");
}
}
}
____________________
Output:
No of Freshman :2
No of Sophomore :1
No of Junior :1
No of Senior :0
Among Freshman Mitchell Beck got the highest gpa
Among Sophomore Thelma Colon got the highest gpa
Among Junior Erma Mullins got the highest
gpa
_____________________
2)
// aboutComputer.txt
A computer is a device that can be instructed to carry out
an
arbitrary set of arithmetic or logical operations
automatically.
The ability of computers to follow a sequence of operations
called a program make computers very flexible and useful.
Since ancient times simple manual devices like the abacus
aided people in doing calculations. Early in the Industrial
Revolution, some mechanical devices were built to automate
long tedious tasks, such as guiding patterns for looms.
________________________
// LettersFrequency.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class LettersFrequency {
public static void main(String[] args) {
int letter[]=new int[26];
String line;
try {
Scanner sc=new
Scanner(new File("aboutComputer.txt"));
while(sc.hasNext())
{
line=sc.nextLine().toLowerCase();
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)>='a'
&& line.charAt(i)<='z')
{
int val = line.charAt(i) -
97;
letter[val]++;
}
}
}
sc.close();
System.out.println("Letter\tCount");
System.out.println("-----\t----");
for(int
i=0;i<letter.length;i++)
{
System.out.println((char)(65+i)+"\t"+letter[i]);
}
} catch (FileNotFoundException e)
{
System.out.println("** File Not Found **");
}
}
}
____________________________
Output:
Letter Count
----- ----
A 39
B 6
C 22
D 12
E 44
F 7
G 6
H 7
I 33
J 0
K 3
L 25
M 14
N 21
O 32
P 10
Q 1
R 22
S 25
T 35
U 19
V 5
W 2
X 1
Y 6
Z 0
_______________________