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

Unit 5: Writing Classes - Review Assignment

Creating a class with instance methods

Create a class named Student with two instance variables: name of type String and age of type int. Write two instance methods: getName and getAge to return the name and age of the student respectively.

You answeredJUST NOW
Java
import java.util.Scanner;
class Student {
String name;
int age;
// Constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// WRITE YOUR CODE HERE
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name = in.nextLine();
int age = in.nextInt();
Student student = new Student(name, age);
System.out.println(student.getName());
System.out.println(student.getAge());
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
You passed all test cases!
STDIN
Jared
20
Expected STDOUT
Jared
20
Your STDOUT
Jared
20
STDIN
John
16
Expected STDOUT
John
16
Your STDOUT
John
16
STDIN
Maddie
17
Expected STDOUT
Maddie
17
Your STDOUT
Maddie
17

Did you like this question?

(Voting helps us personalize your learning experience!)
imgInstructor solution
Alec KretchAPR 22, 2024, 7:04:01 PM
Java
import java.util.Scanner;
class Student {
String name;
int age;
// Constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// Instance method to get the name
public String getName() {
return this.name;
}
// Instance method to get the age
public int getAge() {
return this.age;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name = in.nextLine();
int age = in.nextInt();
Student student = new Student(name, age);
System.out.println(student.getName());
System.out.println(student.getAge());
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Was this helpful?

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

What is the purpose of instance methods in a class?

Select one of the following options:
  • A.

    Instance methods allow objects of a class to perform specific tasks and interact with their data.

  • B.

    Instance methods are used to define static variables in a class.

  • C.

    Instance methods are used to create objects of a class.

  • D.

    Instance methods are used to perform tasks that are not related to objects of a class.

Submit answer

Was this helpful?

(Voting helps us personalize your learning experience!)

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