+ 1
Create a user definded datatype for representing real world object student in java world create 2 student objet with their value
plz
2 Réponses
+ 1
I'm not sure if I understand you correctly or not, but try this?
https://code.sololearn.com/cXQDOdExaS0P/#java
class Student {
private String name;
private int age;
public Student() {
this.name = "John Doe";
this.age = 18;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge() {
return this.age;
}
}
public class Program
{
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student("Jakob Marley", 32);
Student student3 = new Student("Amresh Kanungo", 18);
System.out.println(":::Student 1:::");
System.out.println("Name: " + student1.getName());
System.out.println("Age: " + student1.getAge());
System.out.println("");
System.out.println(":::Student 2:::");
System.out.println("Name: " + student2.getName());
System.out.println("Age: " + student2.getAge());
System.out.println("");
System.out.println(":::Student 3:::");
System.out.println("Name: " + student3.getName());
System.out.println("Age: " + student3.getAge());
System.out.println("");
System.out.println("...Changing Student 1's name to Amir.");
student1.setName("Amir Jo");
student1. setAge(22);
System.out.println("");
System.out.println(":::Student 1:::");
System.out.println("Name: " + student1.getName());
System.out.println("Age: " + student1.getAge());
System.out.println("");
}
}