Hello everyone! Someone can help me with one point, I write a program that takes the ArrayList objects of different classes, namely, from trial_exam, trial_test and trial_final_exam, which are inherited from super class trial, and in order to output these objects with variables , It is necessary to override the Tostring method, but when I override this method in a super class, I can not contact the heir variables, so, is somebody can tell me how to override the method in a super class so that you can take the heirs’ variables?)
Class Main:
import java.io.bufferedReader;
Import java.io.ioException;
Import java.io.InputStreamReader;
Import java.util.arraylist;
Import java.util.list;
Import java.util.scanner;
Public Class Main {
Public Static Void Main (String [] Args) Throws IoException {
STRING NUMBER = NULL;
ArrayList & LT; Trial & GT; TrialaRrayList = New ArrayList & LT; Trial & GT; ();
do {
System.out.PrintLN ("Welcome! Choose the option you are interested in");
System.out.PrintLN ("1. Add a new element");
System.Out.printLN ("2. Remove the index element");
System.Out.printLN ("3. Withdraw all elements in the console");
System.out.PrintLN (4. Comparison of two elements ");
System.out.printLN ("5. Exit");
System.Out.print ("& gt;");
Scanner Scanner = New Scanner (System.in);
do {
BufferedReader Reader = New BufferedReader (new inputStreamReader (System.in));
Number = Reader.ReadLine ();
} While (! Isnumeric (Number) ||! Prov_na_Diap (Number));
//System.out.printLN(NUM_INT);
Switch (Number) {
Case "1":
System.Out.printLN ("OK, let's add a new item, only, click:");
System.out.printLN ("1 - If you want to add an item, such as \" Exam \ "");
System.out.PrintLN ("2 - If you want to add an item, such as \" test \ "");
System.out.PrintLN ("3 - if you want to add an item, such as \" final exam \ "");
do {
BufferedReader Reader = New BufferedReader (new inputStreamReader (System.in));
Number = Reader.ReadLine ();
} While (! isnumeric (Number) ||! Prov_na_Diapazon (Number)); // View later
STRING NAME = NULL;
String Score_Test = NULL;
STRING NAME_STUD = NULL;
int numer = 0;
int score = 0;
int yar = 0;
if (number.equals ("1")) {
System.Out.print ("Enter the name of the ticket:");
Name = Scaner.NextLine ();
System.Out.print ("Enter the rating:");
score = scaner.nextint ();
While (True) {
System.Out.print ("Enter the ticket number:");
If (! scaner.hasnextint ()) {
System.out.PrintLN ("Incorrect data, try again!");
Scaner.NextLine ();
} else {
numer = scaner.nextint ();
Scaner.NextLine ();
Break;
}
}
Trial_exam Exam = New Trial_exam (Name, Numer, Score);
TrialarRayList.add (exam);
System.out.PrintLN (exam.name_of_tick + "" + exam.num_of_tick + "" + exam.score);
}
ELSE If (number.equals ("2")) {
System.Out.print ("Enter the test name:");
Name = Scaner.NextLine ();
System.Out.print ("Enter the estimate (\\ \\ q.)");
Score_Test = Scaner.NextLine ();
While (True) {
System.Out.print ("Enter the number of the option:");
If (! scaner.hasnextint ()) {
System.out.PrintLN ("Incorrect data, try again!");
Scaner.NextLine ();
} else {
numer = scaner.nextint ();
Scaner.NextLine ();
Break;
}
}
Trial_Test Test = New Trial_Test (Name, Numer, Score_Test);
TrialaRrayList.add (test);
System.Out.printLN (test.test_name + "" + test.num_of_var + "" + test.test);
}
ELSE If (Number.equals ("3")) {
System.Out.print ("Enter the name of the Scientist:");
Name = Scaner.NextLine ();
System.Out.print ("Enter the student name:");
name_stud = scaner.nextline ();
While (True) {
System.Out.print ("Enter the year of release:");
If (! scaner.hasnextint ()) {
System.out.PrintLN ("Incorrect data, try again!");
Scaner.NextLine ();
} else {
year = scaner.nextint ();
Scaner.NextLine ();
Break;
}
}
While (True) {
System.Out.print ("Enter the rating:");
If (! scaner.hasnextint ()) {
System.out.PrintLN ("Incorrect data, try again!");
Scaner.NextLine ();
} else {
numer = scaner.nextint ();
Scaner.NextLine ();
Break;
}
}
trial_final_exam final_exam = new trial_final_exam (Name, Name_Stud, Year, Numer);
Trialarraylist.add (Final_exam);
System.out.PrintLN (final_exam.title_of_the_rep + "" + Final_exam.students_name + ""
+ final_exam.year_of_issue + "" + Final_exam.score);
}
Break;
Case "2":
System.out.PrintLN ("OK, let's delete the item, only, click:");
System.out.PrintLN ("1 - If you want to delete an item, type \" Exam \ "");
System.out.PrintLN ("2 - If you want to remove an item, type \" test \ "");
System.out.PrintLN ("3 - If you want to remove an item, like \" final exam \ "");
do {
BufferedReader Reader = New BufferedReader (new inputStreamReader (System.in));
Number = Reader.ReadLine ();
} While (! isnumeric (Number) ||! Prov_na_Diapazon (Number));
if (number.equals ("1")) {
If (trialarraylist.size () == 0) {
System.Out.printLN ("Your list is empty, first of all, add elements to it");
Break;
}
ELSE {
System.Out.printLN ("Your list of items in the list:");
System.out.PrintLN (trialarraylist.tostring ());
}
}
Break;
Case "3":
Break;
Case "4":
Break;
Case "5":
System.Out.printLN ("Thank you for work, goodbye");
Break;
}
} While (integer.paraseint (Number)! = 5);
}
Public Static Boolean Isnumeric (String Str) {
Try {
Double.Parsedouble (STR);
RETURN TRUE;
} Catch (NumberFormateException E) {
System.out.printLN ("Incorrect input, please repeat an attempt");
RETURN FALSE;
}
}
Public Static Boolean Prov_Na_Diap (String Str) {
int str_to_int = integer.paraseint (STR);
if ((STR_TO_INT & GT; = 1 & AMP; STR_TO_INT & LT; = 5)) RETURN TRUE;
ELSE {
System.out.printLN ("Incorrect input, please repeat an attempt");
RETURN FALSE;
}
}
Public Static Boolean Prov_Na_DiaPazon (String Str) {
int str_to_int = integer.paraseint (STR);
if ((STR_TO_INT & GT; = 1 & amp; str_to_int & lt; = 3)) Return True;
ELSE {
System.out.printLN ("Incorrect input, please repeat an attempt");
RETURN FALSE;
}
}
@Override
Public String Tostring () {
Return super.Tostring ();
}
}
Trial class:
Import java.security.publickey;
Import java.util.Objects;
Public Class Trial {
String Test;
INT SCORE;
Public String Tostring (String Tnname, Int Num, String Restest) {
Return "Test for the subject:" + TNName + ", option number:" + Num + ", Evaluation:" + Restest;
}
Public String Tostring (String Nametick, Int Numtic, Int Resexam) {
Return "Tame of the ticket" + Nametick + ", ticket number:" + NUMTIC + ", Rating:" + resexam;
}
Public String Tostring (String Namedio, String Namestud, Int Yearofis, Int Resexam) {
Return "The name of the diploma:" + Namedio + ", the name of the student:" + namestud + ", year of release:" + yearofis + ", Calling:" + Resexam;
}
@Override
Public Boolean Equals (Object O) {
if (this == o) Return True;
if (O == NULL || getClass ()! = O.Getclass ()) Return False;
Trial Trial = (Trial) O;
Return Score == Trial.score & amp; & amp; Objects.equals (test, trial.test);
}
@Override
Public int hashcode () {
Return Objects.hash (Test, Score);
}
@Override
Public String Tostring () {
Return.
}
}
class Trial_Exam:
Public Class Trial_EXAM EXTENDS Trial {
String Name_of_Tick;
INT NUM_OF_TICK;
Trial_Exam (String Name_of_Tick, INT Num_of_Tick, Int Score) {
this.name_of_tick = name_of_tick;
this.num_of_tick = num_of_tick;
this.score = score;
}
Public String Tostring () {
Return "Trial_Exam {" +
"name_of_tick = '" + name_of_tick +' \ '' +
", num_of_tick =" + num_of_tick +
'}';
}
}
Class Trial_Test:
Public Class Trial_Test Extends Trial {
String Test_Name;
INT NUM_OF_VAR;
Trial_Test (String Test_Name, Int Num_of_Var, String Test) {
this.test_name = test_name;
this.num_of_var = num_of_var;
this.Test = Test;
}
@Override
Public String Tostring () {
Return "Trial_Test {" +
"test_name = '" + test_name +' \ '' +
", num_of_var =" + num_of_var +
'}';
}
}
Class Trial_Final_exam:
Public Class Trial_Final_EXAM EXTENDS Trial {
String title_of_the_rep;
String Students_Name;
int year_of_issue;
trial_final_exam (string title_of_the_rep, String Students_Name,
INT YEAR_OF_ISSUE, INT SCORE) {
this.title_of_the_rep = title_of_the_rep;
this.students_name = students_name;
this.year_of_issue = year_of_issue;
this.score = score;
}
@Override
Public String Tostring () {
Return "trial_final_exam {" +
"TITLE_OF_THE_REP = '" + TITLE_OF_THE_REP +' \ '' +
", Students_Name = '" + Students_name +' \ '' +
", year_of_issue =" + year_of_issue +
'}';
}
}
Answer 1, Authority 100%
OK, Well, I figured out, it was necessary to override the Tostring method in each class the heir and then use either iterator or the Tostring method.
If someone needs, again:
Class Main:
import java.io.bufferedReader;
Import java.io.ioException;
Import java.io.InputStreamReader;
Import java.util.arraylist;
Import java.util.iterator;
Import java.util.list;
Import java.util.scanner;
Public Class Main {
Public Static Void Main (String [] Args) Throws IoException {
STRING NUMBER = NULL;
ArrayList & LT; Trial & GT; TrialaRrayList = New ArrayList & LT; Trial & GT; ();
do {
System.out.PrintLN ("Welcome! Choose the option you are interested in");
System.out.PrintLN ("1. Add a new element");
System.Out.printLN ("2. Remove the index element");
System.Out.printLN ("3. Withdraw all elements in the console");
System.out.PrintLN (4. Comparison of two elements ");
System.out.printLN ("5. Exit");
System.Out.print ("& gt;");
Scanner Scanner = New Scanner (System.in);
do {
BufferedReader Reader = New BufferedReader (new inputStreamReader (System.in));
Number = Reader.ReadLine ();
} While (! Isnumeric (Number) ||! Prov_na_Diap (Number));
//System.out.printLN(NUM_INT);
Switch (Number) {
Case "1":
System.Out.printLN ("OK, let's add a new item, only, click:");
System.out.printLN ("1 - If you want to add an item, such as \" Exam \ "");
System.out.PrintLN ("2 - If you want to add an item, such as \" test \ "");
System.out.PrintLN ("3 - if you want to add an item, such as \" final exam \ "");
do {
BufferedReader Reader = New BufferedReader (new inputStreamReader (System.in));
Number = Reader.ReadLine ();
} While (! isnumeric (Number) ||! Prov_na_Diapazon (Number)); // View later
STRING NAME = NULL;
String Score_Test = NULL;
STRING NAME_STUD = NULL;
int numer = 0;
int score = 0;
int yar = 0;
if (number.equals ("1")) {
System.Out.print ("Enter the name of the ticket:");
Name = Scaner.NextLine ();
System.Out.print ("Enter the rating:");
score = scaner.nextint ();
While (True) {
System.Out.print ("Enter the ticket number:");
If (! scaner.hasnextint ()) {
System.out.PrintLN ("Incorrect data, try again!");
Scaner.NextLine ();
} else {
numer = scaner.nextint ();
Scaner.NextLine ();
Break;
}
}
Trial_exam Exam = New Trial_exam (Name, Numer, Score);
TrialarRayList.add (exam);
System.out.PrintLN (exam.name_of_tick + "" + exam.num_of_tick + "" + exam.score);
}
ELSE If (number.equals ("2")) {
System.Out.print ("Enter the test name:");
Name = Scaner.NextLine ();
System.Out.print ("Enter the estimate (\\ \\ q.)");
Score_Test = Scaner.NextLine ();
While (True) {
System.Out.print ("Enter the number of the option:");
If (! scaner.hasnextint ()) {
System.out.PrintLN ("Incorrect data, try again!");
Scaner.NextLine ();
} else {
numer = scaner.nextint ();
Scaner.NextLine ();
Break;
}
}
Trial_Test Test = New Trial_Test (Name, Numer, Score_Test);
TrialaRrayList.add (test);
System.Out.printLN (test.test_name + "" + test.num_of_var + "" + test.test);
}
ELSE If (Number.equals ("3")) {
System.Out.print ("Enter the name of the Scientist:");
Name = Scaner.NextLine ();
System.Out.print ("Enter the student name:");
name_stud = scaner.nextline ();
While (True) {
System.Out.print ("Enter the year of release:");
If (! scaner.hasnextint ()) {
System.out.PrintLN ("Incorrect data, try again!");
Scaner.NextLine ();
} else {
year = scaner.nextint ();
Scaner.NextLine ();
Break;
}
}
While (True) {
System.Out.print ("Enter the rating:");
If (! scaner.hasnextint ()) {
System.out.PrintLN ("Incorrect data, try again!");
Scaner.NextLine ();
} else {
numer = scaner.nextint ();
Scaner.NextLine ();
Break;
}
}
trial_final_exam final_exam = new trial_final_exam (Name, Name_Stud, Year, Numer);
Trialarraylist.add (Final_exam);
System.out.PrintLN (final_exam.title_of_the_rep + "" + Final_exam.students_name + ""
+ final_exam.year_of_issue + "" + Final_exam.score);
}
Break;
Case "2":
System.out.PrintLN ("OK, let's delete the item, only, click:");
System.out.PrintLN ("1 - If you want to delete an item, type \" Exam \ "");
System.out.PrintLN ("2 - If you want to remove an item, type \" test \ "");
System.out.PrintLN ("3 - If you want to remove an item, like \" final exam \ "");
do {
BufferedReader Reader = New BufferedReader (new inputStreamReader (System.in));
Number = Reader.ReadLine ();
} While (! isnumeric (Number) ||! Prov_na_Diapazon (Number));
if (number.equals ("1")) {
If (trialarraylist.size () == 0) {
System.Out.printLN ("Your list is empty, first of all, add elements to it");
Break;
}
ELSE {
System.Out.printLN ("Your list of items in the list:");
ITERATOR & LT; Trial & GT; trialiterator = trialarraylist.iterator ();
While (trialiterator.hasnext ()) {
Trial NextTrial = trialiterator.next ();
System.out.PrintLN ("-" + NextTrial);
}
}
}
Break;
Case "3":
Break;
Case "4":
Break;
Case "5":
System.Out.printLN ("Thank you for work, goodbye");
Break;
}
} While (integer.paraseint (Number)! = 5);
}
Public Static Boolean Isnumeric (String Str) {
Try {
Double.Parsedouble (STR);
RETURN TRUE;
} Catch (NumberFormateException E) {
System.out.printLN ("Incorrect input, please repeat an attempt");
RETURN FALSE;
}
}
Public Static Boolean Prov_Na_Diap (String Str) {
int str_to_int = integer.paraseint (STR);
if ((STR_TO_INT & GT; = 1 & AMP; STR_TO_INT & LT; = 5)) RETURN TRUE;
ELSE {
System.out.printLN ("Incorrect input, please repeat an attempt");
RETURN FALSE;
}
}
Public Static Boolean Prov_Na_DiaPazon (String Str) {
int str_to_int = integer.paraseint (STR);
if ((STR_TO_INT & GT; = 1 & amp; str_to_int & lt; = 3)) Return True;
ELSE {
System.out.printLN ("Incorrect input, please repeat an attempt");
RETURN FALSE;
}
}
@Override
Public String Tostring () {
Return super.Tostring ();
}
}
Trial class:
Import java.security.publickey;
Import java.util.Objects;
Public Class Trial {
String Test;
INT SCORE;
Public String Tostring (String Tnname, Int Num, String Restest) {
Return "Test for the subject:" + TNName + ", option number:" + Num + ", Evaluation:" + Restest;
}
Public String Tostring (String Nametick, Int Numtic, Int Resexam) {
Return "Tame of the ticket" + Nametick + ", ticket number:" + NUMTIC + ", Rating:" + resexam;
}
Public String Tostring (String Namedio, String Namestud, Int Yearofis, Int Resexam) {
Return "The name of the diploma:" + Namedio + ", the name of the student:" + namestud + ", year of release:" + yearofis + ", Calling:" + Resexam;
}
@Override
Public Boolean Equals (Object O) {
if (this == o) Return True;
if (O == NULL || getClass ()! = O.Getclass ()) Return False;
Trial Trial = (Trial) O;
Return Score == Trial.score & amp; & amp; Objects.equals (test, trial.test);
}
@Override
Public int hashcode () {
Return Objects.hash (Test, Score);
}
@Override
Public String Tostring () {
Return "Trial {" +
"Test = '" + Test +' \ '' +
", score =" + score +
'}';
}
}
class Trial_Exam:
Public Class Trial_EXAM EXTENDS Trial {
String Name_of_Tick;
INT NUM_OF_TICK;
Trial_Exam (String Name_of_Tick, INT Num_of_Tick, Int Score) {
this.name_of_tick = name_of_tick;
this.num_of_tick = num_of_tick;
this.score = score;
}
Public String Tostring () {
Return "Trial_Exam {" +
"name_of_tick = '" + name_of_tick +' \ '' +
", num_of_tick =" + num_of_tick +
", score =" + score +
'}';
}
}
Class Trial_Test:
Public Class Trial_Test Extends Trial {
String Test_Name;
INT NUM_OF_VAR;
Trial_Test (String Test_Name, Int Num_of_Var, String Test) {
this.test_name = test_name;
this.num_of_var = num_of_var;
this.Test = Test;
}
@Override
Public String Tostring () {
Return "Trial_Test {" +
"test_name = '" + test_name +' \ '' +
", num_of_var =" + num_of_var +
", Test =" + Test +
'}';
}
}
Class Trial_Final_exam:
Public Class Trial_Final_EXAM EXTENDS Trial {
String title_of_the_rep;
String Students_Name;
int year_of_issue;
trial_final_exam (string title_of_the_rep, String Students_Name,
INT YEAR_OF_ISSUE, INT SCORE) {
this.title_of_the_rep = title_of_the_rep;
this.students_name = students_name;
this.year_of_issue = year_of_issue;
this.score = score;
}
@Override
Public String Tostring () {
Return "trial_final_exam {" +
"TITLE_OF_THE_REP = '" + TITLE_OF_THE_REP +' \ '' +
", Students_Name = '" + Students_name +' \ '' +
", year_of_issue =" + year_of_issue +
", score =" + score +
'}';
}
}