import java.io.FileWriter; import java.io.IOException; import java.util.Scanner;
ID: 3917369 • Letter: I
Question
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Student {
static String name;
static int grade;
static double gpa;
static boolean exit = true;
static String exitProgram;
public static void main(String[] args) throws IOException {
FileWriter names = new FileWriter("C:\Users\Students\Desktop\files ames.csv",true);
FileWriter grades = new FileWriter("C:\Users\Students\Desktop\files\grades.csv",true);
FileWriter gpas = new FileWriter("C:\Users\Students\Desktop\files\gpas.csv",true);
Scanner input = new Scanner(System.in);
do {
System.out.print("Enter student name: ");
name = input.nextLine();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
System.out.print("Enter student grade: ");
grade = input.nextInt();
System.out.print("Enter student GPA: ");
gpa = input.nextDouble();
names.append(name+", ");
grades.append(grade+", ");
gpas.append(gpa+", ");
System.out.print("Exit? (y/n): ");
exitProgram = input.next().toLowerCase().trim();
input.nextLine();
if (exitProgram.equals("n")) {
exit = false;
names.close();
grades.close();
gpas.close();
input.close();
}
} while (exit == true);
}
}
Explanation / Answer
If you have any doubts, please give me comment...
import java.io.*;
import java.util.*;
class Student {
private String name;
private int grade;
private double gpa;
Student(String name, int grade, double gpa){
this.name = name;
this.grade = grade;
this.gpa = gpa;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param gpa the gpa to set
*/
public void setGpa(double gpa) {
this.gpa = gpa;
}
/**
* @param grade the grade to set
*/
public void setGrade(int grade) {
this.grade = grade;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the grade
*/
public int getGrade() {
return grade;
}
/**
* @return the gpa
*/
public double getGpa() {
return gpa;
}
public String getInfo(){
return "My name is "+name+". I'm in grade "+grade+" and I have a "+gpa+" GPA.";
}
}
public class StudentDemo {
public static void main(String[] args) throws IOException {
Scanner inNames = new Scanner(new File("names.csv"));
Scanner inGrades = new Scanner(new File("grades.csv"));
Scanner inGpas = new Scanner(new File("gpas.csv"));
String names[] = new String[10];
double gpas[] = new double[10];
int grades[] = new int[10];
for(int i=0; i<10; i++){
names[i] = inNames.nextLine();
names[i] = names[i].substring(0, 1).toUpperCase() + names[i].substring(1);
grades[i] = inGrades.nextInt();
gpas[i] = inGpas.nextDouble();
}
Student []students = new Student[10];
for(int i=0; i<10; i++){
students[i] = new Student(names[i], grades[i], gpas[i]);
System.out.println(students[i].getInfo());
}
inNames.close();
inGrades.close();
inGpas.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.