d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Java Check? > Inside.
Add Reply New Topic New Poll
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
May 4 2013 11:04am
Ok so this is for school - but some reason I keep getting not the desired output - I think I messed up somewhere but after looking it over a few times it just looks more and more right sooo Ima ask yall.


Its not showing the toString() in my testDrive- it will show the ' blah ' in the loop so i know the loop works :X

Code
import java.util.*;

public class Part1TestDrive {

   public static void main(String[] args) {
   
   
   // Part1Kinder Part1Student -- Doc names (easy copy pasta)
   Part1Student student01 = new Part1Student();
   
   student01.setName("Teddy");
   student01.setID("1234");
   student01.setSchool("Randolph");
   student01.setTeacher("Mr. Larry");
   
   ArrayList<Part1Student> list = new ArrayList<Part1Student>();
   list.add(student01);
   

// Goes through items in array list - calls the toString() for the current list item
       for (int i = 0; i < list.size(); i++) {
           list.get(i).toString();
           System.out.println("BLAH");
       }
   
   }
}

Student class
Code
import java.util.*;

public class Part1Student {

   private String _Studentid;
   private String _StudentName;
   private String _SchoolName;
   private String _Teacher;


  public void setID(String Studentid) {
       _Studentid = Studentid;
   }

   public String getID() {
       return _Studentid;
   }
   
   public void setName (String StudentName) {
       _StudentName = StudentName;
   }
   
   public String getName() {
       return _StudentName;
   }
   
   public void setSchool (String SchoolName) {
       _SchoolName = SchoolName;
   }
   
   public String getSchool() {
       return _SchoolName;
   }
   
      public void setTeacher (String Teacher) {
       _Teacher = Teacher;
   }
   
   public String getTeacher() {
       return _Teacher;
   }

    public String toString() {
         return "StudentID: " + _Studentid
         + "\nStudentName: " + _StudentName + "\nSchoolName: "
         + _SchoolName + "\nTeacher: " + _Teacher;
    }
}


Kindergarden students subclass

Code
import java.util.*;

public class Part1Kinder extends Part1Student {

   private String _showTellItem;
   
   public void getNewField (String showTellItem) {
       _showTellItem = showTellItem;
   }
   public String setNewField() {
       return _showTellItem;
   }
   
   public String toString() {
   
       // Part1Student ref01 = new Part1Student();
   
       return /* ref01.toString() + */ "\nA Kindergardn Student!\nShowTellItem: "
       + _showTellItem;
   }
}
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
May 4 2013 11:05am
Or maybe i just simply misread my assignment (specs below)

Create a class that holds data for a general student.
Create instance variables for student id, name, school, extracurricular activities, and homeroom teacher.
Create get and set methods for the instance variables.
Create a method named toString() that has no parameters and return a string with the data about this class.
Next, create a subclass of the first class for kindergarten students. This class will add new field and override a method.
Add a new instance variable for show and tell item. Create get and set methods for the field.
Override the toString() method and output information on kindergarten students. Label the student as a kindergarten student and include the show and tell item.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 4 2013 11:14am
tldr didn't read all that. just read your problem

replace this:
list.get(i).toString();

with this:
System.out.println(list.get(i).toString());
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
May 4 2013 11:24am
Quote (carteblanche @ May 4 2013 12:14pm)
tldr didn't read all that. just read your problem

replace this:
list.get(i).toString();

with this:
System.out.println(list.get(i).toString());


AWSOME! Thanks... i cant belive i missed that ... I looked over this for like 2 hrs :D all i needed was some fresh eyes i guess :)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll