I need to get data and show it.
This is my question
Construct a class designed to perform that takes student record containing Roll Number, Name and Marks
as data and functions like get()and show() to take input in data and display data.
I am trying to do like this
import java.util.Scanner;
class Student {
String name;
String stu_id;
int score;
public Student() {
this(" ", " ", 0);
}
public Student(String initName, String initId, int initScore) {
name = initName;
stu_id = initId;
score = initScore;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Input number of students:");
int n = Integer.parseInt(in.nextLine().trim());
System.out.println("Input Student Name, ID, Score:");
Student stu = new Student();
for (int i = 0; i < n; i ++) {
stu.name = in.next();
stu.stu_id = in.next();
stu.score = in.nextInt();
System.out.println(stu.name + " " + stu.stu_id);
}
System.out.println("name, ID of the highest score and the lowest score:");
System.out.println(stu.name + " " + stu.stu_id);
in.close();
}
}
But its wrong I just need to create a function show() on which ill get data and from get() function it will just print
It seems to me that this solution is acceptable, you will try to rewrite it later from memory, this is how I began to learn to program.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Student {
String name;
String stu_id;
int score;
public Student() {
this("None", "None", 0);
}
public Student(String initName, String initId, int initScore) {
name = initName;
stu_id = initId;
score = initScore;
}
#Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", stu_id='" + stu_id + '\'' +
", score=" + score +
'}';
}
}
public class Main {
public static Student get(Scanner in) {
System.out.println("Input Student Name, ID, Score:");
String name = in.next();
int score = in.nextInt();
String stu_id = in.next();
return new Student(name, stu_id, score);
}
public static void show(Student student) {
System.out.println(student);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Input number of students:");
int n = Integer.parseInt(in.nextLine().trim());
List<Student> studentList = new ArrayList<>();
for (int i = 0; i < n; i ++) {
Student stu = get(in);
studentList.add(stu);
}
studentList.forEach(student -> {show(student);});
in.close();
}
}
import java.util.Scanner;
class Student {
String name;
String stu_id;
int score;
Scanner in = new Scanner(System.in);
public void get()
{
System.out.println("Enter Student Name, ID, Score:");
name = in.nextLine();
stu_id = in.nextLine();
score = in.nextInt();
}
public void show()
{
System.out.println("Name: "+name+"\nId: "+stu_id+"\nScore: "+score);
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Input number of students:");
int n = in.nextInt();
in.nextLine();
Student[] stu = new Student[n];
for (int i = 0; i < n; i++) {
stu[i] = new Student();
stu[i].get();
}
for (int i = 0; i < n; i++) {
stu[i].show();
}
in.close();
}
}
Related
**Hi everyone. I'm new in this platform and I need some help with my code in JAVA.
There's this error in the code and I don't know how to solve it.
Can anyone help me with this?**
import java.util.*;
public class Q3 {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Exam e[]= new Exam[5];
for(int i=1;i<=5; i++)
{
e[i]=new Exam();
}
for(int i=1;i<=5;i++)
{
System.out.println("Enter the details of the student: His name, course and roll no.
respectively:");
String name=sc.nextLine();
String course=sc.nextLine();
int roll=sc.nextInt();
System.out.println("Enter the mark1, mark2 and mark3 respectively:");
int mark1=sc.nextInt();
int mark2=sc.nextInt();
int mark3=sc.nextInt();
e[i].input_Student(roll, name,course);
e[i].input_Marks(mark1, mark2, mark3);
}
System.out.println("The result is displayed below:");
for(int i=1; i<=5;i++)
{
e[i].display_Student();
e[i].display_Result();
** This is where I'm facing the problem. It says-The method display_Marks() is undefined for the
type Exam
- The method display_Result() is undefined for the
type Exam**
}
}
}
class Student
{
int roll;
String name;
String course;
public void input_Student(int roll, String name, String course)
{
this.roll=roll;
this.name=name;
this.course=course;
}
void display_Student()
{
System.out.println("Roll no:"+roll+", Name:"+name+", Course"+course);
}
class Exam extends Student
{
int mark1, mark2,mark3;
void input_Marks(int mark1, int mark2, int mark3)
{
this.mark1=mark1;
this.mark2=mark2;
this.mark3=mark3;
}
void display_Result()
{
System.out.println("mark1:"+mark1+", mark2:"+mark2+", mark3:"+mark3);
}
}
}
One way to solve the issue is by making Exam class static.
But it is recommended to make Exam a separate class instead of being a nested in Student class
public class Q3 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Exam e[] = new Exam[5];
for (int i = 1; i <= 5; i++) {
e[i] = new Exam();
}
for (int i = 1; i <= 5; i++) {
System.out.println("Enter the details of the student: His name, course and roll no. respectively:");
String name = sc.nextLine();
String course = sc.nextLine();
int roll = sc.nextInt();
System.out.println("Enter the mark1, mark2 and mark3 respectively:");
int mark1 = sc.nextInt();
int mark2 = sc.nextInt();
int mark3 = sc.nextInt();
e[i].input_Student(roll, name, course);
e[i].input_Marks(mark1, mark2, mark3);
sc.nextLine();
}
System.out.println("The result is displayed below:");
for (int i = 1; i <= 5; i++) {
e[i].display_Student();
e[i].display_Result();
}
}
}
class Student {
int roll;
String name;
String course;
public void input_Student(int roll, String name, String course) {
this.roll = roll;
this.name = name;
this.course = course;
}
void display_Student() {
System.out.println("Roll no:" + roll + ", Name:" + name + ", Course" + course);
}
}
class Exam extends Student {
int mark1, mark2, mark3;
void input_Marks(int mark1, int mark2, int mark3) {
this.mark1 = mark1;
this.mark2 = mark2;
this.mark3 = mark3;
}
void display_Result() {
System.out.println("mark1:" + mark1 + ", mark2:" + mark2 + ", mark3:" + mark3);
}
}
Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score.
I stuck at how do I display their name?
Here's my code:
package Exercises;
import java.util.Scanner;
public class Page93
{
public static void main(String[] args)
{
String name = null;
int count;
double score = 0;
double highest = 0;
Scanner input = new Scanner (System.in);
System.out.print("Enter the number of student : ");
int numberofstudent = input.nextInt();
for (count=0; count<numberofstudent; count++)
{
System.out.print("\nStudent name : ");
name = input.next().toUpperCase();
System.out.print("Score : ");
score = input.nextInt();
if (highest<score)
highest=score;
}
System.out.print("\nThe highest score : " + highest );
}
}
Define a variable studentWithHighestScore to store Student with the highest score. Update this variable whenerver you update highest.
if (highest<score) {
highest=score;
studentWithHighestScore = name
}
package Exercises;
import java.util.Scanner;
public class Page93
{
public static void main(String[] args)
{
String name = null;
int count;
double score = 0;
double highest = 0;
String highestName;
Scanner input = new Scanner (System.in);
System.out.print("Enter the number of student : ");
int numberofstudent = input.nextInt();
for (count=0; count<numberofstudent; count++)
{
System.out.print("\nStudent name : ");
name = input.next().toUpperCase();
System.out.print("Score : ");
score = input.nextInt();
if (highest<score)
{
highest=score;
highestName = name;
}
}
System.out.print("\nThe highest student : " + highestName + " score : " + highest );
}
}
import java.util.Scanner;
class Student {
String name;
String stu_id;
int score;
public Student() {
}
public Student(String initName, String initId, int initScore) {
name = initName;
stu_id = initId;
score = initScore;
}
}
class accept {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Input number of students:");
int n = Integer.parseInt(in.nextLine().trim()) ;
System.out.println("Input Student Name, ID, Score :");
Student stu = new Student();
Student max = new Student();
Student min = new Student("","", 0);
String [] arr1=new String [n];
String [] arr2=new String [n];
int [] arr3=new int [n];
for (int i = 0; i < n; i ++) {
arr1[i]=in.next();
arr2[i]=in.next();
arr3[i]=in.nextInt();
stu.name = arr1[i];
stu.stu_id = arr2[i];
stu.score = arr3[i];
if (max.score < stu.score) {
max.name = stu.name;
max.stu_id = stu.stu_id;
max.score = stu.score; }}
for(int j = 0; j < n; j ++){
stu.name = arr1[j];
stu.stu_id = arr2[j];
stu.score = arr3[j];
if (min.score < stu.score&&stu.score!=max.score) {
min.name = stu.name;
min.stu_id = stu.stu_id;
min.score = stu.score;
}
}
System.out.println("name, ID of the highest score and the second highest score:");
System.out.println(max.name + " " + max.stu_id);
System.out.println(min.name + " " + min.stu_id);
in.close();
}
}
I need both the String and double arrays to go from inputAccept to table:
inputAccept();
table(names, costs);
public static void inputAccept() {
Scanner scan = new Scanner(System.in);
String[] names = {"","","","","","","","","",""};
double[] costs = new double[10];
for (int i = 0; i < 10; i++)
{
System.out.println("Enter the name of item " + (i + 1) + ": ");
names[i] = scan.nextLine();
System.out.println("Enter item cost: ");
costs[i] = scan.nextDouble();
}
}
public static void table(String[] names, double[] costs) {
//insert code using the arrays
}
I know this is wrong, but I don't know what to do to fix it.
You can just create the two arrays in the main method, then first pass them into the inputAccept method, then pass the two arrays into the table method:
public static void main(String[] args) {
double[] costs = new double[10];
String[] names = {"","","","","","","","","",""};
inputAccept(names, costs);
table(names, costs);
}
public static void inputAccept(String[] names, double[] costs) {
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
System.out.println("Enter the name of item " + (i + 1) + ": ");
names[i] = scan.nextLine();
System.out.println("Enter item cost: ");
costs[i] = scan.nextDouble();
}
}
public static void table(String[] names, double[] costs) {
//insert code using the arrays
}
First it looks like you are creating "items" from user input. I think modeling a class Item should be a first step:
public final class Item {
private final String name;
private final double cost;
public Item(String name, double cost) {
this.name = name;
this.cost = cost;
}
public String getName() {
return name;
}
public double getCost() {
return cost;
}
}
Then inputAccept() becomes:
public static Item[] inputAccept() {
Item[] items = new Item[10];
try (Scanner scan = new Scanner(System.in)) {
for (int i = 0; i < items.length; i++) {
System.out.println("Enter the name of item " + (i + 1) + ": ");
String name = scan.next();
System.out.println("Enter item cost: ");
double cost = scan.nextDouble();
items[i] = new Item(name, cost);
}
}
return items;
}
Therefore, table(...) becomes
public static void table(Item[] items) {
// insert code using the arrays
}
And the final usage is
public static void main(String[] args) {
table(inputAccept());
}
public class Main {
static String[] names = {"","","","","","","","","",""};
static double[] costs = new double[10];
public static void main(String[] args) {
inputAccept();
table(names, costs);
}
public static void inputAccept() {
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
System.out.println("Enter the name of item " + (i + 1) + ": ");
names[i] = scan.next();
System.out.println("Enter item cost: ");
costs[i] = scan.nextDouble();
}
}
public static void table(String[] names, double[] costs) {
for (String name: names) {
System.out.println(name);
}
for (double value: costs) {
System.out.println(value);
}
}
}
This approach is as clean as a baby's bottom
private class InputResult {
//generate constructor with both fields
String[] names; //generate getter
double[] costs; //generate getter
}
then
public static InputResult inputAccept()
...
return new InputResult(names, costs);
public static void table(InputResult input)
String[] names = input.GetNames();
...
So i have 3 classes, and one that runs all the code. A student class, a Graduate student class and an undergraduate class, and a class called lab 4 that runs the code. The code asks for how many grad students and how many undergrad students there are, then asks the user to input all the information for that number of each. After all the info is inputed, the under grad or grad student objects are added to an array list of students. after all the students are added, then prints all the information. However when i input all the information, the array does not print, the program terminates. How do i get the array to print out?
Code:
Lab 4: creates the student objects, array list, adds them to the array, and the prints all information for objects added to the array list
import java.util.ArrayList;
import java.util.Scanner;
public class Lab04 {
public static void main(String[] args) {
ArrayList <Student> studentList = new ArrayList <Student>();
Scanner s = new Scanner(System.in);
System.out.println("How many Graduate Students do you want to store?");
int numOfStudents = s.nextInt();
s.nextLine();
for (int i = 0; i < numOfStudents; i++) {
System.out.println("What is the student's name?");
String name = s.nextLine();
System.out.println("What is the student's GPA?");
double GPA = s.nextDouble();
s.nextLine();
System.out.println("What is the student's ID?");
String ID = s.nextLine();
System.out.println("What is the student's High School?");
String highSchool = s.nextLine();
System.out.println("What is the student's graduate major?");
String gradMajor = s.nextLine();
System.out.println("What is the student's undergraduate major?");
String underMajor = s.nextLine();
System.out.println("What is the student's undergradute school? ");
String underSchool = s.nextLine();
GraduateStudent gradStu = new GraduateStudent(name, GPA, ID, highSchool, gradMajor, underMajor,
underSchool);
studentList.add(gradStu);
}
System.out.println("How many UnderGraduate students are there?");
int numOfUnder = s.nextInt();
s.nextLine();
for (int j = 0; j < numOfUnder; j++) {
System.out.println("What is the student's name?");
String name = s.nextLine();
System.out.println("What is the student's GPA?");
double GPA = s.nextDouble();
s.nextLine();
System.out.println("What is the student's ID?");
String ID = s.nextLine();
System.out.println("What is the student's High School?");
String highSchool = s.nextLine();
System.out.println("What is the student's major?");
String major = s.nextLine();
System.out.println("What the student's minor?");
String minor = s.nextLine();
UnderGraduate UnderGrad = new UnderGraduate(name, GPA, ID, highSchool, major, minor);
studentList.add(UnderGrad);
}
for (int j = 0; j < studentList.size(); j++) {
(studentList.get(j)).getStudentInformation();
}
}
}
Student Class:
public class Student {
private String name;
private double gpa;
private String id;
private String highSchool;
//private String major;
//private String minor;
public Student() {
name = "";
gpa = 0.0;
id = "";
highSchool = "";
//major = "";
//minor = "";
}
public Student(String name, double gpa, String id, String highSchool){
this.name = name;
this.gpa = gpa;
this.id = id;
this.highSchool = highSchool;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getHighSchool() {
return highSchool;
}
public void setHighSchool(String highSchool) {
this.highSchool = highSchool;
}
public String getStudentInformation() {
String result = "Name: " + name + " GPA: " + gpa + " ID: " + id
+ " High School: " + highSchool;
return result;
}
}
graduate Class:
public class GraduateStudent extends Student {
private String gradMajor;
private String underMajor;
private String underSchool;
public GraduateStudent(String name, double gpa, String id, String highSchool,
String gradMajor, String underMajor, String underSchool) {
super(name, gpa, id, highSchool);
this.gradMajor = gradMajor;
this.underMajor = underMajor;
this.underSchool = underSchool;
}
public String getGradMajor() {
return gradMajor;
}
public void setGradMajor(String gradMajor) {
this.gradMajor = gradMajor;
}
public String getUnderMajor() {
return underMajor;
}
public void setUnderMajor(String underMajor) {
this.underMajor = underMajor;
}
public String getUnderSchool() {
return underSchool;
}
public void setUnderSchool(String underSchool) {
this.underSchool = underSchool;
}
#Override
public String getStudentInformation() {
String result = super.getStudentInformation()+
"Graduate Major: " + gradMajor + "Undergraduate Major: " + underMajor +
"Undergraduate School: " + underSchool;
return result;
}
}
Because you're not printing anything. Change
for (int j = 0; j < studentList.size(); j++) {
(studentList.get(j)).getStudentInformation();
}
to
for (int j = 0; j < studentList.size(); j++) {
System.out.println((studentList.get(j)).getStudentInformation());
}
When you ask getStudentInformation, it returns a String, what you want to do now is to System.out.println(...) this String object
Each time I run this code it gets to where it asks for student id and it prints out the student id part and the homework part. Why? I am trying to do get a string for name, id, homework, lab, exam, discussion, and project then in another class I am splitting the homework, lab, and exam strings into arrays then parsing those arrays into doubles. After I parse them I total them in another method and add the totals with project and discussion to get a total score.
import java.util.Scanner;
import java.io.*;
public class GradeApplication_Kurth {
public static void main(String[] args) throws IOException
{
Student_Kurth one;
int choice;
boolean test = true;
do
{
Scanner keyboard = new Scanner(System.in);
PrintWriter outputFile = new PrintWriter("gradeReport.txt");
System.out.println("Please select an option: \n1. Single Student Grading \n2. Class Grades \n3. Exit");
choice = keyboard.nextInt();
switch (choice)
{
case 1 :
System.out.println("Please enter your Student name: ");
String name = keyboard.next();
System.out.println("Please enter you Student ID: ");
String id = keyboard.nextLine();
System.out.println("Please enter the 10 homework grades seperated by a space: ");
String homework = keyboard.next();
System.out.println("Please enter the 6 lab grades seperated by a space: ");
String lab = keyboard.nextLine();
System.out.println("Please enter the 3 exam grades seperated by a space: ");
String exam = keyboard.nextLine();
System.out.println("Please enter the discussion grade: ");
double discussion = keyboard.nextDouble();
System.out.println("Please enter the project grade: ");
double project = keyboard.nextDouble();
one = new Student_Kurth(name, id, homework, lab, exam, discussion, project);
outputFile.println(one.toFile());
System.out.println(one);
break;
case 2 :
File myFile = new File("gradeReport.txt");
Scanner inputFile = new Scanner(myFile);
while(inputFile.hasNext())
{
String str = inputFile.nextLine();
System.out.println("\n" + str);
}
break;
case 3 :
test = false;
keyboard.close();
outputFile.close();
System.exit(0);
}
} while (test = true);
}
}
second class
public class Student_Kurth
{
public String homework;
public String name;
public String id;
public String lab;
public String exam;
public double project;
public double discussion;
public double[] hw = new double[10];
public double[] lb = new double[6];
public double[] ex = new double[3];
public final double MAX = 680;
public double percentage;
public String letterGrade;
public Student_Kurth()
{
homework = null;
name = null;
id = null;
lab = null;
exam = null;
project = 0;
discussion = 0;
}
public Student_Kurth(String homework, String name, String id, String lab, String exam, double project, double discussion)
{
this.homework = homework;
this.name = name;
this.id = id;
this.lab = lab;
this.exam = exam;
this.project = project;
this.discussion = discussion;
}
public void Homework(String homework)
{
String delims = " ";
String[] tokens = this.homework.split(delims);
int tokenCount = tokens.length;
for(int i = 0; i < tokenCount; i++)
{
hw[i] = Double.parseDouble(tokens[i]);
}
}
public void Lab(String lab)
{
String delims = " ";
String[] tokens = this.lab.split(delims);
int tokenCount = tokens.length;
for(int i = 0; i < tokenCount; i++)
{
lb[i] = Double.parseDouble(tokens[i]);
}
}
public void Exam(String exam)
{
String delims = " ";
String[] tokens = this.exam.split(delims);
int tokenCount = tokens.length;
for(int i = 0; i < tokenCount; i++)
{
ex[i] = Double.parseDouble(tokens[i]);
}
}
public double getHomeworkTotal(double[] hw)
{
double hwTotal = 0;
for(int i = 0; i < hw.length; i++)
{
hwTotal += hw[i];
}
return hwTotal;
}
public double getLabTotal(double[] lb)
{
double lbTotal = 0;
for(int i = 0; i < lb.length; i++)
{
lbTotal += lb[i];
}
return lbTotal;
}
public double getExamTotal(double[] ex)
{
double exTotal = 0;
for(int i = 0; i < ex.length; i++)
{
exTotal += ex[i];
}
return exTotal;
}
public double getTotalScores(double getExamTotal, double getLabTotal, double getHomeworkTotal)
{
return getExamTotal + getLabTotal + getHomeworkTotal + this.project + this.discussion;
}
public double getPercentage(double getTotalScores)
{
return 100 * getTotalScores / MAX;
}
public String getLetterGrade(double getPercentage)
{
if(getPercentage > 60)
{
if(getPercentage > 70)
{
if(getPercentage > 80)
{
if(getPercentage > 90)
{
return "A";
}
else
{
return "B";
}
}
else
{
return "C";
}
}
else
{
return "D";
}
}
else
{
return "F";
}
}
public void getLetter(String getLetterGrade)
{
letterGrade = getLetterGrade;
}
public void getPercent(double getPercentage)
{
percentage = getPercentage;
}
public String toFile()
{
String str;
str = " " + name + " - " + id + " - " + percentage + " - " + letterGrade;
return str;
}
public String toString()
{
String str;
str = "Student name: " + name + "\nStudent ID: " + id + "\nTotal Score: " + getTotalScores(getExamTotal(ex), getLabTotal(lb), getHomeworkTotal(hw)) +
"\nMax Scores: " + MAX + "Percentage: " + percentage + "Grade: " + letterGrade;
return str;
}
}
At the end of the switch, you have
while ( test = true)
You probably want to change that to
while ( test == true)
Also, take these lines out of the loop:
Scanner keyboard = new Scanner(System.in);
PrintWriter outputFile = new PrintWriter("gradeReport.txt");
In addition to Ermir's answer, this line won't capture all the grades:
System.out.println("Please enter the 10 homework grades seperated by a space: ");
String homework = keyboard.next();
Keyboard.next only reads until the next delimiter token, so if you want to capture 10 grades separated by spaces you need capture the whole line, like:
System.out.println("Please enter the 10 homework grades separated by a space: ");
String homework = keyboard.nextLine();