I need a little help with my application. I want to make a student tracker application in Java. The application should be able to add new students, years, courses, grades and average for grades. I'm using eclipse mars for it.
Can you please tell me how to make the average for grades, and how to assign new year, courses, and grades for every student, please?
Thank you.
enter code here
public Student(String name, String year, String[] course, int [] grades) {
super();
this.name = name;
this.year = year;
Course = course;
Grades = grades;
}
private String name;
private String year;
private String[] Course;
private int [] Grades;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String [] getCourse() {
return Course;
}
public void setCourse(String[] course) {
Course = course;
}
public int [] getGrades() {
return Grades;
}
public void setGrades(int [] grades) {
Grades = grades;
}
//import (default package).Student;
import java.util.Scanner;
public class MainApplication {
public static void main(String[] args) {
String [] w = {"Mate","Fizica"};
int [] x = {8,9,10};
Student a = new Student("Ene Cristian","Anul 2", w ,x);
String [] y = {"Math", "Info"};
int [] z = {7, 10 ,11};
Student b = new Student("Popscu Ion","Anul 1", w, x);
System.out.println(a.getName());
System.out.println(a.getYear());
System.out.println(b.getName());
System.out.println(b.getYear());
}
}
public class Course {
private String Courses;
}
Extend your Student class with the following method:
public int getAverageGrade(){
int averageGrade = 0;
for(int grade : Grades){
averageGrade = averageGrade + grade;
}
averageGrade = averageGrade/Grades.length;
return averageGrade;
}
Afterwards you can print the average grade using:
System.out.println(a.getAverageGrade());
Related
I am this close to finally completing this assignment, but I've hit a roadblock. For some reason I can't get call aspects of the class in my methods. Does anyone have a way of doing this that works? I've put comments next to the three parts I'm referring to. I do not know what is causing these errors, and Im grateful for any help.
public class Main
{
public static int w = -1;
public static int x = 0;
public static int y = 0;
public static int z = 0;
public static String[] names;
public static int[] years;
public static String[] studios;
public static class Movie{
// instance variables
private int year;
private String title;
private String studio;
private int placement;
// Constructor for objects of class Movie
Movie(String title, int year, String studio)
{
// initialize instance variables
this.title = title;
this.year = year;
this.studio = studio;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public void setPlacement() {
w+=1;
this.placement = w;
}
public int getPlacement() {
return placement;
}
public String getStudio()
{
return studio;
}
public void setStudio(String studio)
{
this.studio = studio;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public String toString()
{
String str = String.format("%-30s %4d %-20s", title, year, studio);
return str;
}
}
public static void arrNames(String name) {
x+=1;
names = new String[x];
names[(x-1)] = name.getTitle(); //error here, why can't I just use getTitle()?
}
public static void arrYear(String name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
public static void arrStudio(String name) {
z+=1;
studios = new String[z];
studios[z-1] = name.getStudio(); //same error
}
public static void setGroup(String name) {
arrNames(name);
arrYear(name);
arrStudio(name);
}
public static void getGroups(String name) {
int a = x;
int b = y;
int c = z;
int d = 0;
int e = 0;
int f = 0;
System.out.println("Names Category: ");
while (a > 0) {
System.out.print(names[d] + " ");
d+=1;
a-=1;
}
System.out.println("Years Category: ");
while (b > 0) {
System.out.print(years[e] + " ");
e+=1;
b-=1;
}
System.out.println("Studios Category: ");
while (c > 0) {
System.out.print(studios[f] + " ");
f+=1;
c-=1;
}
public static void main(String[] args) {
Movie nemo = new Movie("Finding Nemo",2003,"Pixar");
setGroup(nemo.getTitle());
}
}
I will take this exemple:
public static void arrYear(String name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
You are trying to use the method getYear on the variable name and the name is a String and not a Movie Object
You could just replace the argument from String to Movie like that:
public static void arrYear(Movie name) {
y+=1;
years = new int[y];
years[(y-1)] = name.getYear(); //same error
}
and it should work for all your functions
I am trying to use a Scanner object to take in user input that will then be attributed to the following values for an object called TargetHeartRateCalculator.
TargetHeartRateCalculator takes constructor parameters of..
String fName, String lName, int month, int day, int year.
Normally without using the Scanner I would just instantiate the object with the parameters manually like...
TargetHeartRateCalculator patient1 = new TargetHeartRateCalculator("Tom", "Willickers", 01, 25, 1966);
I need to use the Scanner to take user input and then from the user recived input assign values to fName, lName, month, day, year.
I have tried making the object instantiation part of the assignment of the user input through the Scanner object by the syntax is not correct and I'm not even really sure if that's how your supposed to do something like this.
I feel like this is probably a simple solution but the answer is quite evasive to me.
Here is my Driver Class...
import java.util.Scanner;
public class DriverClass {
//--------------------------------------------------------------------------------------
// TARGET HEART RATE CALCULATOR CLASS FUNCTIONALITY TEST
//--------------------------------------------------------------------------------------
TargetHeartRateCalculator patient1 = new TargetHeartRateCalculator("Tom", "Willickers", 01, 25, 1966);
TargetHeartRateCalculator patient2 = new TargetHeartRateCalculator("Bill", "Skarsgard", 8,9, 1990);
//Write a java app that prompts for the persons information
//instantiates an object and prints the information from that object
//first name, last name, date of birth,
// calculates maximum heart rate, and target heart rate.
//and then displays them to the them.
Scanner input = new Scanner(System.in);
System.out.print("Please enter your first name: ");
String fnInput = input.nextDouble();
System.out.printf("%nThank you %s %s. Your date of birth is %n" +
"%d and you are %d years old!%n" +
"Your maximum heart rate is %.2f and your %n" +
"Target Heart Rate range is %.2f%n%n" get,
patientInfo(patient1);
patientInfo(patient2);
displayAgeYears(patient1);
displayAgeYears(patient2);
displayMaxHeartRate(patient1);
displayMaxHeartRate(patient2);
displayTargetHeartRate(patient1);
displayTargetHeartRate(patient2);
}
}
Here is my Class
import java.time.LocalDateTime;
public class TargetHeartRateCalculator {
private String fName;
private String lName;
private int dOB;
private int bMonth;
private int bDay;
private int bYear;
private int ageYears;
private double maxHeartRate;
private double lowTargetHearRate;
private double highTargetHearRate;
LocalDateTime now = LocalDateTime.now();
int cYear = now.getYear();
int cMonth = now.getMonthValue();
int cDay = now.getDayOfMonth();
//constructor
public TargetHeartRateCalculator(String fNameIn, String lNameIn, int monthIn, int dayIn, int yearIn) {
fName = fNameIn;
lName = lNameIn;
bMonth = monthIn;
bDay = dayIn;
bYear = yearIn;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public int getBMonth() {
return bMonth;
}
public void setBMonth(int month) {
this.bMonth = month;
}
public int getBDay() {
return bDay;
}
public void setBDay(int day) {
this.bDay = day;
}
public int getBYear() {
return bYear;
}
public void setBYear(int year) {
this.bYear = year;
}
public int getAgeYear(){
int currentAgeYear = cYear -bYear;
return currentAgeYear;
}
public int getAgeMonth(){
int currentAgeMonth =cMonth - bMonth;
return currentAgeMonth;
}
public int getAgeDay(){
int currentAgeDay =cDay - bDay;
return currentAgeDay;
}
public String getdOB(TargetHeartRateCalculator patient) {
String dOB = String.format("%s/%s/%s",
patient.getBMonth(), patient.getBDay(), patient.getBYear());
return dOB;
}
public void setdOB(int dOB) {
this.dOB = dOB;
}
public static String displayAgeYears(TargetHeartRateCalculator patient) {
String ageYears = String.format("%s %s is %s Years old",
patient. getfName(), patient.getlName(), patient.getAgeYear());
return ageYears;
}
public void setAgeYears(int ageYears) {
this.ageYears = ageYears;
}
public double getMaxHeartRate() {
double maxHeartRate = (220 - getAgeYear()) ;
return maxHeartRate;
}
public void setMaxHeartRate(int maxHeartRate) {
this.maxHeartRate = maxHeartRate;
}
public double getLowTargetHearRate() {
lowTargetHearRate = getMaxHeartRate() * .5;
return lowTargetHearRate;
}
public void setLowTargetHearRate(int lowTargetHearRate) {
this.lowTargetHearRate = lowTargetHearRate;
}
public double getHighTargetHeartRate(){
highTargetHearRate = getMaxHeartRate() * .85;
return highTargetHearRate;
}
public void setHighTargetHearRate(){
this.highTargetHearRate = highTargetHearRate;
}
public static String displayTargetHeartRate(TargetHeartRateCalculator patient){
String hRateRange = String.format("%.2f BPM - %.2f BPM", patient.getLowTargetHearRate(), patient.getHighTargetHeartRate());
return hRateRange;
}
public static String displayMaxHeartRate(TargetHeartRateCalculator patient){
String mHeartRate = String.format("%.2f BPM", patient.getMaxHeartRate());
return mHeartRate;
}
public static String patientInfo(TargetHeartRateCalculator patient) {
String result = String.format("Patient Name: %s %s DOB: %d/%d/%d",
patient.getfName(), patient.getlName(), patient.getBMonth(), patient.getBDay(), patient.getBYear());
return result;
}
}
You should read all variables you need to construct your TargetHeartRateCalculator instance.
System.out.print("Please enter your first name: ");
String firstName = input.next();
System.out.print("Please enter your lastname: ");
String lastName = input.next();
System.out.print("Please enter your birthday day: ");
String birthdayDay = input.nextInt();
System.out.print("Please enter your birthday month: ");
String birthdayMonth = input.nextInt();
System.out.print("Please enter your birthday year: ");
String birthdayYear = input.nextInt();
TargetHeartRateCalculator patient = TargetHeartRateCalculator(firstName, lastName,
birthdayDay, birthdayMonth, birthdayYear);
Then, you can call your static methods after initialized your TargetHeartRateCalculator.
TargetHeartRateCalculator.displayTargetHeartRate(patient);
But, instead of putting everything into a class and using static methods in TargetHeartRateCalculator, you should divide your TargetHeartRateCalculator into two which can be Patient and TargetHeartRateCalculator.
Patient class can be like this:
import java.time.LocalDateTime;
public class Patient {
private String fName;
private String lName;
private int dOB;
private int bMonth;
private int bDay;
private int bYear;
private int ageYears;
private int currentYear = LocalDateTime.now().getYear();
private int currentMonth = LocalDateTime.now().getMonthValue();
private int currentDay = LocalDateTime.now().getDayOfMonth();
public Patient(String fName, String lName, int bMonth, int bDay, int bYear) {
this.fName = fName;
this.lName = lName;
this.bMonth = bMonth;
this.bDay = bDay;
this.bYear = bYear;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public int getbMonth() {
return bMonth;
}
public void setbMonth(int bMonth) {
this.bMonth = bMonth;
}
public int getbDay() {
return bDay;
}
public void setbDay(int bDay) {
this.bDay = bDay;
}
public int getbYear() {
return bYear;
}
public void setbYear(int bYear) {
this.bYear = bYear;
}
public int getAgeYear(){
return currentYear -bYear;
}
public int getAgeMonth(){
return currentMonth - bMonth;
}
public int getAgeDay(){
return currentDay - bDay;
}
}
The calculations will be in another class that take Patient object as an parameter and make calculations.
If I understand correctly, you're trying to get input into the constructor? I'll show one field since the rest are all the same
Scanner input = new Scanner(System.in);
System.out.print("Please enter your first name: ");
String fnInput = input.nextLine();
// repeat print and scan, as needed
TargetHeartRateCalculator patient1 = TargetHeartRateCalculator(fnInput); // add more parameters
Then you'll want have instance methods, not static methods with parameters.
public void patientInfo() {
System.out.printf("%s%n", this.getfName()); // for example
}
You call those methods like this
patient1.patientInfo();
And I think you should remove the print statements from the main method
I'm not sure y this coding is not receiving the input for the adjust variable and gets terminated before it runs completely
public class Question {
int id;
String name;
String type;
double amt;
public Question(int id, String name, String type, double amt) {
this.id = id;
this.name = name;
this.type = type;
this.amt = amt;
}
public static void main(String[] args)
{ }
}
import java.util.*;
public class Answer {
public static void gettype(Question[] q,String adjust)
{
for(int i=0;i<2;i++)
{
if(q[i].getType()==adjust)
{
System.out.println(q[i].getId());
}
}}
public static void main(String[] args) {
int id;
String name,type,adjust;
double amt;
Scanner s=new Scanner(System.in);
Answer a=new Answer();
System.out.println("enter 2 car inputs");
Question[] q=new Question[2];
for(int i=0;i<2;i++)
{
id=s.nextInt();
s.nextLine();
name=s.nextLine();
type=s.nextLine();
amt=s.nextDouble();
q[i]= new Question(id,name,type,amt);
}
adjust=s.nextLine();
a.gettype(q,adjust);
}
}
While running the code i am able to get the inputs for the car object array.But after that i am not able to get the values for the variable adjust.
So please need help with this one.
I have tried simply to print the objects at the constructor side.
But not able to receive the 9th input which will be assigned to the var adjust
I think this is better. You should understand why mine works.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Question {
private int id;
private String name;
private String type;
private double amt;
Question(int id, String name, String type, double amt) {
this.id = id;
this.name = name;
this.type = type;
this.amt = amt;
}
int getId() {
return id;
}
String getName() {
return name;
}
String getType() {
return type;
}
double getAmt() {
return amt;
}
#Override
public String toString() {
final StringBuffer sb = new StringBuffer("Question{");
sb.append("id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append(", type='").append(type).append('\'');
sb.append(", amt=").append(amt);
sb.append('}');
return sb.toString();
}
}
class Answer {
public static final int NUM_QUESTIONS = 2;
public static void main(String[] args) {
int numQuestions = (args.length > 0) ? Integer.valueOf(args[0]) : NUM_QUESTIONS;
List<Question> questions = new ArrayList<>();
Scanner s = new Scanner(System.in);
for (int i = 0; i < numQuestions; ++i) {
System.out.println(String.format("Question %d", i));
System.out.print("id: ");
int id = s.nextInt();
System.out.print("name: ");
String name = s.next();
System.out.print("type: ");
String type = s.next();
System.out.print("amt: ");
double amt = s.nextDouble();
questions.add(new Question(id, name, type, amt));
}
System.out.println(questions);
}
}
public class Student {
int rollNumber;
String name;
int noOfSubjects;
ArrayList<Subject> subjectList = new ArrayList<>();
public TestStudent(int rollNumber, String name, int noOfSubjects) {
super();
this.rollNumber = rollNumber;
this.name = name;
this.noOfSubjects = noOfSubjects;
}
// inner class
public class Subject {
String subjectName;
int marks;
public Subject(String subjectname, int marks) {
super();
subjectName = subjectname;
this.marks = marks;
}
public int getSubjectMarks() {
return marks;
}
}
public void addSubject(Subject s) {
subjectList.add(s);
}
public int getMarks(Subject s) {
return s.getSubjectMarks();
}
like in above class Student ,there is an inner class Subject which contains inforamtion of subject names and corresponding marks student has got so if i want to print all subject names and respective marks along with the student details , how can we do it ?
Can someone help solving this. I want to print all the object once please
public class Student {
private static String firstName;
private static String lastName;
private static int studentId;
private static String major;
private static double balance;
public Student (String fName, String lName,int id,String mjr,double blce) {
firstName = new String(fName);
lastName = new String(lName);
studentId = id;
major = new String(mjr);
balance = blce;
}
public String toString () {
return firstName + "\t" + lastName + "\t" + studentId + "\t" + major + "\t$" + balance;
}
public boolean equals (Object obj) {
if (obj instanceof Student) {
Student collegeStud = (Student) obj;
return (this.firstName.equals(collegeStud.firstName));
} else
return false;
}
public static String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public static String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public static int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public static String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public static double getBalance() {
return balance;
}
/*
* .commmm
*/
public void setBalance(double balance) {
this.balance = balance;
}
public static void main (String[] args) {
Student Mike = new Student ("Mike","Versace", 99, "CS",0.00);
Student John = new Student ("John","Sling" ,97, "Maths", 20.00);
Student Bob = new Student ("Bob","Tomson" ,57, "Physic",5.00);
System.out.println (Mike.toString() + "\n" + John.toString());
if (Mike.equals(John))
System.out.println ("Mike is John");
else
System.out.println ("Mike is NOT John");
}
}
import java.io.ObjectInputStream.GetField;
public class StudentList {
private int numberOfStudents=0;
private Student[] studentListArray;
//private int studentCount = 0;
StudentList () {
numberOfStudents=0;
studentListArray = new Student[100];
}
public void createStudent(String firstName, String lastName,int studentId, String major, double balance){
Student collegeStud = new Student(firstName, lastName, studentId, major, balance);
addStudent(collegeStud);
numberOfStudents++;
}
public void addStudent (Student collegeStud) {
studentListArray[numberOfStudents++]=new Student(collegeStud.getFirstName(), collegeStud.getLastName(),
collegeStud.getStudentId(), collegeStud.getMajor(),collegeStud.getBalance());
}
public String toString() {
String result = "";
for (int i=0; i<numberOfStudents; i++) {
result += studentListArray[i].toString() + "\n";
}
return result;
}
public Student[] getList() {
return studentListArray;
}
public int listSize() {
return numberOfStudents;
}
public Student searchForStudent (String firstName){
int index = 0;
while (index < numberOfStudents) {
if (studentListArray[index].equals(new Student(Student.getFirstName(),Student.getLastName(),Student.getStudentId(),Student.getMajor(),Student.getBalance()))) {
return studentListArray[index];
}
index++;
}
return null;
}
public static void main(String args[]) {
StudentList theList = new StudentList();
theList.addStudent (new Student ("John","Sling" ,97, "Maths", 20.00));
theList.addStudent (new Student ("Mike","Versace", 99, "CS",0.00));
theList.addStudent (new Student ("Bob","Tomson" ,57, "Physic",5.00));
//theList.createStudent(new Student(Student.getFirstName(),Student.getLastName(),Student.getStudentId(),Student.getMajor(),Student.getBalance()));
//theList.searchForStudent(new String());
System.out.println (theList.toString());
}
}
The problem is that you marked your fields as static. Remove it and the method will work as expected.
public class Student {
//non-static fields
private String firstName;
private String lastName;
private int studentId;
private String major;
private double balance;
//similar for getters, setters and toString method
}
Static members are shared amongst all objects in a class rather than being one per object. Hence each new object you create is overwriting the data of the previous one.
More info:
What does the 'static' keyword do in a class?