0
Can someone help me? why my code is not working?
I spend long time on trying to solve it myself but its still not working. Where is the mistake? help me pls... https://code.sololearn.com/ca119A99a56a/#java the instructions: The program you are given receives name and age of student as input. Complete the program to set the values for the corresponding attributes of the Student class and prints out the final result. If the age is <0, program should output "Invalid age" and assign a 0 value to the age attribute. Sample Input Olivia -2 Sample Output Invalid age Name: Olivia Age: 0
21 odpowiedzi
0
Maja Jasinska
Here is your correct solution
https://code.sololearn.com/cAnyG1KUDfnX/?ref=app
+ 9
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);
//Please Subscribe to My Youtube Channel
//Channel Name: Fazal Tuts4U
if(age < 0){
System.out.println("Invalid age");
System.out.println("Name: " + student.name);
System.out.println("Age: " + 0);
}else{
System.out.println("Name: " + student.name);
System.out.println("Age: " + student.getAge());
}
}
}
class Student {
public String name;
public int age;
public int getAge() {
return this.age;
//this keyword
}
public void setAge(int age) {
this.age = age;
}
}
+ 6
Try this one
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);
if(age < 0){
student.setAge(0);
System.out.println("Invalid 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
this.age = age;
}
}
+ 3
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
System.out.println("enter name");
String name = read.next();
System.out.println("enter age");
int age = read.nextInt();
Student student = new Student();
student.setAge(age);
student.setName(name);
if(student.getAge() <= 0){
System.out.println("Invalid Age");
}else {
System.out.println("Name: "+student.getName());
System.out.println("Age: "+student.getAge());
}
}
}
class Student{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
+ 3
Yes, you are good to go 👍 ,
Bravo 👏
But in my opinion I think you should set the age to 0 through setter and output it via getter.
But all in all you succeeded even without that approach.
+ 2
You need to put the checking in setAge()
In your current flow,
setAge() is called before getAge(),
and with input of -2, setAge() updates the age to 0,
then in getAge(),
the INVALID AGE condition will never be true, because age have been corrected to 0.
See the logic?
+ 2
Try this code
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);
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;
private int age;
public int getAge() {
//complete Getter
if (age < 0){
return age = 0;
}else{
return age;
}
}
public void setAge(int age) {
//complete Setter
if (this.age < 0){
this.age = 0;
} else {
this.age = age;
}
}
}
its working fine ....
+ 2
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);
//Please Subscribe to My Youtube Channel
//Channel Name: Fazal Tuts4U
if(age < 0){
System.out.println("Invalid age");
System.out.println("Name: " + student.name);
System.out.println("Age: " + 0);
}else{
System.out.println("Name: " + student.name);
System.out.println("Age: " + student.getAge());
}
}
}
class Student {
public String name;
public int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
+ 2
Thanks
+ 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;
student.setAge(age);
//Please Subscribe to My Youtube Channel
//Channel Name: Fazal Tuts4U
if(age < 0){
System.out.println("Invalid age");
System.out.println("Name: " + student.name);
System.out.println("Age: " + 0);
}else{
System.out.println("Name: " + student.name);
System.out.println("Age: " + student.getAge());
}
}
}
class Student {
public String name;
public int age;
public int getAge() {
return this.age;
//this keyword
}
public void setAge(int age) {
this.age = age;
}
}
+ 1
I don't know if the above code working for you 🤔
+ 1
Can you try my code in your system?
The getter and setter concept must fulfill the encapsulation concept and never try to access data directly
+ 1
The getter and setter concept is good, but according the the question you supposed to output your results as they recommend.
Try my solution above 👆
0
@Gordon thank you for your reply! I see it now :) however I still don't get the output at all... no mater if I use age -2 or for example 10. Is there any other mistake or my correction is wrong?
0
@Gordon
@AJ
Thanks so much!! I apreciate you taking time to reply to my question!
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);
if(age < 0){
System.out.println("Invalid age");
System.out.println("Name: " + student.name);
System.out.println("Age: " + 0);
}else{
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 this.age;
}
public void setAge(int age) {
//complete Setter
this.age=age;
}
}
This should work
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");
student.setAge(0);
}
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 = age;
}
}
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);
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;
private int age;
public int getAge() {
//complete Getter
if (age < 0){
return age = 0;
}else{
return age;
}
}
public void setAge(int age) {
//complete Setter
if (this.age < 0){
this.age = 0;
} else {
this.age = age;
}
}
}
//this is working fine
0
This code only runs test 1,3,4 correctly: I don't have a clue other than the if else statements are mandatory. Allow me to go back and try again. The solution shared is not working either. Phew!
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.setAge(age);
student.name = name;
if(age < 0){
student.setAge(0);
System.out.println("Invalid Age");
}
//set the age via Setter
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;
//complete Getter
}
public void setAge(int age) {
this.age = age;
//complete Setter
}
}
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);
if(age < 0){
student.setAge(0);
System.out.println("Invalid 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
this.age = age;
}
}