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

Unit 2: Using Objects - Review Assignment

Creating and Using an Object with a Custom Constructor

Write a program that creates a new object of a class Dog with the attributes name and age. The Dog class has been provided for you. Create a new Dog object using the custom constructor, set its name to "Rex" and age to 5, and print the name and age.

You answeredJUST NOW
Java
import java.io.*;
import java.util.*;
class Dog {
String name;
int age;
Dog(String name, int age) {
this.name = name;
this.age = age;
}
String getName() {
return this.name;
}
int getAge() {
return this.age;
}
}
public class CodingQuestion {
public static void main(String[] args) {
//WRITE YOUR CODE HERE
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
You passed all test cases!
STDIN
Expected STDOUT
Rex
5
Your STDOUT
Rex
5

Did you like this question?

(Voting helps us personalize your learning experience!)
imgInstructor solution
Alec KretchAPR 21, 2024, 1:09:24 PM
Java
import java.io.*;
import java.util.*;
class Dog {
String name;
int age;
Dog(String name, int age) {
this.name = name;
this.age = age;
}
String getName() {
return this.name;
}
int getAge() {
return this.age;
}
}
public class CodingQuestion {
public static void main(String[] args) {
//WRITE YOUR CODE HERE
Dog myDog = new Dog("Rex", 5);
System.out.println(myDog.getName());
System.out.println(myDog.getAge());
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Was this helpful?

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

How do you create a new object using a custom constructor in Java?

Select one of the following options:
  • A.

    By using the new keyword followed by the class name.

  • B.

    By calling the custom constructor without the new keyword.

  • C.

    By using the new keyword followed by the custom constructor with appropriate arguments.

  • D.

    By declaring a variable of the class type and assigning it the values of the attributes.

Submit answer

Was this helpful?

(Voting helps us personalize your learning experience!)

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