+ 1

Student information system

I'm new to setters and getters so I have no idea what's going on here. This code is supposed to print "Invalid age" if age entered < 0. Then it's supposed to set the age attribute to 0. import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String name = read.nextLine(); int age = read.nextInt(); Student student = new Student(); student.name = name; //set the age via Setter student.age = age; if (age < 0){ System.out.println("Invalid age"); } System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { public String name; public int age; public int getAge() { //complete Getter return age; } public void setAge(int age) { //complete Setter this.age = 0; }

14th Nov 2021, 12:07 AM
Natanael
13 Respostas
+ 5
Also, your Student classes fields (name, age) should be private when using getters and setters, so that they cannot be accessed directly. I.E. student.name and student.age should result in compile time errors, if done correctly, and the only way to access the fields from outside the class should be through the classes getters and setters.
14th Nov 2021, 1:37 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Have you went through getters and setters in the Java lesson yet? The slides provide clear examples of how those are used to get and set attributes. Your getter looks ok. Setter on the other hand, should be where the logic of setting age to 0 if age is less than 0 resides. public void setAge(int age) { this.age = age; if (age < 0) this.age = 0; } You may then proceed to call setter in main. student.setAge(age);
14th Nov 2021, 12:30 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Natanael It looks like your Student class got cut off, otherwise it is missing a closing brace for the setAge() method. Anyway, in the setAge() method you need to also handle what to do if the passed age variable isn't less than 0 to properly set the age of the student. Missing the getter and setter methods for the Student name. In the Main class you have the System.out.println()'s as part of the class body. These need to be inside a method to run. Place them inside the main() method at the end. You should not be using student.name or student.age at all, but rather calls to the getter and setter methods of the student instance. Here's a working example of your code when you're ready to look at it. I suggest that you try to solve your mistakes with the provided info first. Then look at the code. https://code.sololearn.com/cgT8AN4EcccV/?ref=app
14th Nov 2021, 6:38 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
'this' refers to the current instance of the class. this.age = age; is needed to avoid ambiguity between the age variable of the class/instance and the age variable/parameter that is passed as an argument. class Student { ... private int age; // use 'this' keyword to insure you're accessing the fields of the instance. ... ... public void setAge(int age) // Do not use 'this' keyword if you want to refer to the age variable here. age = 0; works because there isn't any ambiguity as it will infer that the instance age is implied.
14th Nov 2021, 10:36 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Great thanks so much, I will try to figure it out with your instructions first
14th Nov 2021, 7:28 PM
Natanael
+ 1
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String name = read.nextLine(); int age = read.nextInt(); Student student = new Student(); student.name = name; //set the age via Setter student.setAge(age); System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { public String name; private int age; public int getAge() { //complete Getter return age; } public void setAge(int age) { //complete Setter if (age < 0) { System.out.println("Invalid age"); age = 0; // also works as this.age = 0; } else { this.age = age; } } }
14th Nov 2021, 9:48 PM
Natanael
+ 1
Thank you
16th Nov 2021, 8:26 PM
Natanael
0
Yes Hatsy Rei, I have gone through getters and setters in the Java lesson but no it's not clear at all to someone who's never seen this before, it's clear like everything else if you're familiar with it. I did as you mentioned but now the code has no output. import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String name = read.nextLine(); int age = read.nextInt(); Student student = new Student(); student.name = name; //set the age via Setter student.age = age; } System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { private String name; private int age; public int getAge() { //complete Getter return age; } public void setAge(int age) { //complete Setter if (age < 0){ this.age = 0; System.out.println("Invalid age"); }
14th Nov 2021, 5:39 PM
Natanael
0
Thanks ChaoticDawg, I was able to do this but I had to look at your code and reset this code. Please look at this working code, before the else statement, the variable age can be age = 0 or this.age = 0; but after the else statement it has to be this.age = 0 Or else the code won't work right, positive integers will not be displayed. It seems that using setter and getter adds a lot of lines of code just to do something simple like input a name and age but I appreciate this exercise to learn a way to protect variables import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String name = read.nextLine(); int age = read.nextInt(); Student student = new Student(); student.name = name; //set the age via Setter student.setAge(age); System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student {
14th Nov 2021, 9:47 PM
Natanael
0
ChaoticDawg this is what I meant at the end of the code, I'm posting this because I'm not clear between the difference of age = 0; and this.age = 0; age = 0; // also works as this.age = 0; } else { age = age; //works with negative input //this.age = age; makes code work correctly
14th Nov 2021, 9:57 PM
Natanael
0
this code doesn't work idk why import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String name = read.nextLine(); int age = read.nextInt(); Student student = new Student(); student.name = name; student.setAge(age); System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { public String name; private int age; public int getAge() { return age ; } public void setAge(int age) { if (age<0){ System.out.println("invalid age"); this.age=0; } else this.age=age; } }
4th Dec 2021, 7:44 PM
isslem
0
Thank you. Thank you very much for the information on student information systems. For me, this will be very useful in terms of writing my written papers at university. Recently, I do not cope with all the tasks I receive at the university. That is why I turn to various resources that can help in this. For example, on the site https://edubirdie.com/academic-ghostwriting-service I read a review of Ghostwriting services for academic and non-academic purposes.
10th May 2022, 6:51 PM
Boston Gray
0
Hiring someone for writing might gain you some time. Therefore, I will leave this link https://essayreviewking.com/killerpapers-org-review/ for anyone who may need a verified service. The writers are outstanding.
4th Sep 2023, 11:03 PM
Beazy