You can use this assignment in your class!
Mastery Progress(100.0%)
i

Unit 5: Writing Classes - Review Assignment

Implementing Access Modifiers in Java

Create a class named Person with the following fields: name (String), age (int), and address (String). The name field should be public, the age field should be private, and the address field should be protected. Also, create a public method display that prints the name and age of the person in the following format:

Name: <name>
Age: <age>
You answeredJUST NOW
Java
import java.util.Scanner;
class Person {
// Your code here!
}
// Testing mechanism - do not modify
public class Main {
public static void main(String[] args) {
Person person = new Person();
Scanner in = new Scanner(System.in);
person.name = in.nextLine();
person.setAge(in.nextInt());
person.display();
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
You passed all test cases!
STDIN
John
25
Expected STDOUT
Name: John
Age: 25
Your STDOUT
Name: John
Age: 25
STDIN
Sarah
16
Expected STDOUT
Name: Sarah
Age: 16
Your STDOUT
Name: Sarah
Age: 16
STDIN
Mitch
18
Expected STDOUT
Name: Mitch
Age: 18
Your STDOUT
Name: Mitch
Age: 18

Did you like this question?

(Voting helps us personalize your learning experience!)
imgInstructor solution
Alec KretchAPR 22, 2024, 7:20:48 PM
Java
import java.util.Scanner;
class Person {
public String name;
private int age;
protected String address;
public void setAge(int age) {
this.age = age;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Testing mechanism - do not modify
public class Main {
public static void main(String[] args) {
Person person = new Person();
Scanner in = new Scanner(System.in);
person.name = in.nextLine();
person.setAge(in.nextInt());
person.display();
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Was this helpful?

(Voting helps us personalize your learning experience!)
Think you've got it?

Which of the following statements about access modifiers in Java is correct?

Select one of the following options:
  • A.

    The protected access modifier restricts access to the class itself, public allows access from any class, and private allows access within the same package and subclasses.

  • B.

    All access modifiers (public, private, protected) restrict access to the class itself.

  • C.

    The public access modifier restricts access to the class itself, private allows access from any class, and protected allows access within the same package and subclasses.

  • D.

    The private access modifier restricts access to the class itself, public allows access from any class, and protected allows access within the same package and subclasses.

Submit answer

Was this helpful?

(Voting helps us personalize your learning experience!)

You may exit out of this review and return later without penalty.