1 Replies - 6610 Views - Last Post: 10 March 2011 - 08:15 PM Rate Topic: -----

#1 n_soares4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-February 11

Inheritance and toString Method in Java ( I have wall clock and alarm

Posted 10 March 2011 - 08:06 PM

Inheritance and toString Method in Java

I have wall clock and alarm clock. But Alarm clock will match time from wall clock.

On the tester program I don't know how to display the new time for the alarm clock.

My program keeps displaying the previous time entered.

//********************************************************************
// ElectronicsTest.java Author: Lewis and Loftus
//
// Solution to Programming Project 8.12
//********************************************************************
public class ElectronicsTest
{
  //-----------------------------------------------------------------
  //  Creates and exercises a group of objects representing
  //  electronic devices.
  //-----------------------------------------------------------------
  public static void main (String[] args)
  {
    Walkman walkman = new Walkman ("Panasonic", 35.48, 1.5, 97.9,
                                   "The Eagles - Greatest Hits");
    CellPhone cell = new CellPhone ("Motorola", 99.99, .5, 
                                    "555-987-1829", true);
    Printer printer = new Printer ("HP", 145.87, 8.0, 1080, false,
                                   false);
    Clock clock = new Clock ("Centurian", 20.34, 3.2, "12:35pm");
    AlarmClock aClock = new AlarmClock ("Timex", 18.99, 2.3, "1:37pm", 
                                        "6:35am");
    WallClock wClock = new WallClock ("Swiss", 13.89, 2.5, "2:33pm");
    
    System.out.println ("Walkman\n");
    walkman.setFrequency (104.3);
    walkman.setTape ("U2 - The Joshua Tree");
    System.out.println (walkman.toString() + "\n");
    
    System.out.println ("Cell Phone\n");
    System.out.println (cell.toString());
    if (cell.takesPictures())
      System.out.println ("This is a camera phone.\n");
    else
      System.out.println ("Sorry, your phone cannot take pictures.\n");
    
    System.out.println ("Printer\n");
    System.out.println (printer.toString());
    if (printer.printerStatus())
      System.out.println ("This printer is ready to print.\n");
    else
      System.out.println ("Printer not ready. Load ink and paper.\n");
    
    System.out.println ("Clock\n");
    System.out.println (clock.toString() + "\n");
    
    System.out.println ("Wall Clock\n");
    System.out.println ("Current time: " + wClock.getTime());
    System.out.println ("New time will be set to 3:45pm.");
    wClock.setTime("3:45pm");
    System.out.println (wClock.toString() + "\n");
    aClock.setTime(wClock.getTime());
    System.out.println ("Alarm Clock\n");
    System.out.println ("Current time: " + aClock.getTime());
    System.out.println ("New time will match wall clock.");
    System.out.println (aClock.toString() + "\n");
  }
}


public class Clock extends HomeAppliance
{
  private String currentTime;
  private String room;
  
  public Clock(String x_manufacturer, double x_price, double x_weight, String x_currentTime)
  {
    super(x_manufacturer, x_price, x_weight);
    currentTime=x_currentTime;
  }
 
  public void setTime(String x_currentTime){
    currentTime=x_currentTime;
  }
   public String getTime(){
    return currentTime;
  }
  public  void setRoom(String x_room)
  {
    room=x_room;
  }
  
  public String room()
  {
    return "Living Room";
  }
  public String toString(){
    String result= super.toString();
    result+="Current Time: "+ getTime()+"\n";
    result +="In room: "+ room()+"\n";
    return result;
  }
}



public class AlarmClock extends Clock
{
  private String alarmTime;
  
  public AlarmClock(String x_manufacturer, double x_price, double x_weight, String x_currentTime,String x_alarmTime)
  {
    super(x_manufacturer, x_price, x_weight, x_currentTime);
    alarmTime=x_alarmTime;
  }
  public String getTime(){
    return super.getTime();
  }
  public void setTime(String x_alarmTime){
    alarmTime=x_alarmTime;
  }
  public String room()
  {
    return "Bedroom";
  }
  public String toString(){
    String result= super.toString();
    result+="Alarm Time: "+alarmTime+"\n";
    return result;
  }
}


public class WallClock extends Clock
{
  //private String alarmTime;
  
 public WallClock(String x_manufacturer, double x_price, double x_weight,String x_currentTime)
  {
   super(x_manufacturer,x_price,x_weight,x_currentTime);
  }
   public String room()
  {
    return "Kitchen";
  }
   public String toString(){
    return super.toString();
  }
}


Please :code:

This post has been edited by pbl: 10 March 2011 - 08:10 PM
Reason for edit:: Code tags fixed for a newbie


Is This A Good Question/Topic? 0
  • +

Replies To: Inheritance and toString Method in Java ( I have wall clock and alarm

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Inheritance and toString Method in Java ( I have wall clock and alarm

Posted 10 March 2011 - 08:15 PM

This is completly useless and shows a bad understanding of the concept

public String toString(){  
    return super.toString();  
}  


If a subclass does not implement a method, the father's method will be invoked anyway :^:

And what does the HomeAppliance toString() method looks like ? As it will be invoked for all the other classes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1