Student information system | Sololearn: Learn to code for FREE!

+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; }

11/14/2021 12:07:31 AM

Natanael

12 Answers

New Answer

+3

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.

+2

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);

+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

+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.

+1

Great thanks so much, I will try to figure it out with your instructions first

+1

Thank you

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"); }

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 {

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.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; } } }

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

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; } }

0

Thank you