OOP Lab Task 2

De la WikiLabs
Versiunea din 2 noiembrie 2013 17:30, autor: Rhobincu (discuție | contribuții) (Pagină nouă: == Required Tutorials == * The Object Oriented Paradigm; Classes and Objects * Notions About the Java Language * Writing and Executing a Java Program * [[Java Syntax; A P...)
(dif) ← Versiunea anterioară | Versiunea curentă (dif) | Versiunea următoare → (dif)
Jump to navigationJump to search

Required Tutorials

Requirements

  • Add two constructors to class Message, accessible to all classes, one that should have arguments for all three fields (receiver, sender și content), and one that should only have arguments for sender and content, receiver keeping its default null value;
  • Add access modifiers to the class members so that:
    • the three fields should only be accessible in the class; encapsulate them by adding getters, accessible to all classes;
    • method print() should be accessible in all classes;
  • Override toString() method (defined in java.lang.Object class) so that it returns a character string formed similarly with the chat messages: sender: receiver;
  • Create a class called SystemMessage which extends class Message. SystemMessage must contain an extra field of type int called type, only accessible inside the class, and a getter for this field, accessible to all classes;
  • Add two constructors to class SystemMessage, accessible to all classes, one that should only take an argument for initializing field "type", and the second for initializing both "type" and "sender" (see Java Syntax; A Program's Lexical Structure#Class Constructors and Java Syntax; A Program's Lexical Structure#this and super Keywords).
  • Add three class constants to class SystemMessage, of type int, having values 0, 1 and 2, named TYPE_ERROR, TYPE_OK, and TYPE_CHANGE_NAME;
  • Override method toString() defined in class Message, so that it only returns the value of field "type", as a String;
  • Change the instantiations in method main in class TestClass so that they use the new constructors. Add three more instantiations of object of type SystemMessage and display them in the console by using the toString() method.