Hacks Week 3
public abstract class Templates {
public final String masterType = "Template";
private String type;
public interface KeyTypes {
String name();
}
protected abstract KeyTypes getKey();
public String getMasterType() {
return masterType;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public abstract String toString();
public static void print(Templates[] objs) {
System.out.println(objs.getClass() + " " + objs.length);
if (objs.length > 0) {
Templates obj = objs[0];
System.out.println(
obj.getMasterType() + ": " +
obj.getType() +
" listed by " +
obj.getKey());
}
for(Object o : objs)
System.out.println(o);
System.out.println();
}
}
public class Messages extends Generics {
public static KeyTypes key = KeyType.title;
public static void setOrder(KeyTypes key) { Messages.key = key; }
public enum KeyType implements KeyTypes {title, sender, timestamp, text}
private final String sender;
private final int timestamp;
private final String text;
public Messages(String sender, int timestamp, String text)
{
super.setType("Messages");
this.sender = sender;
this.timestamp = timestamp;
this.text = text;
}
// overrides data from parent class
@Override
protected KeyTypes getKey() { return Messages.key; }
/* 'Generics' requires toString override
* toString provides data based off of Static Key setting
*/
@Override
public String toString()
{
String output="";
if (KeyType.sender.equals(this.getKey())) {
output += this.sender;
} else if (KeyType.timestamp.equals(this.getKey())) {
output += "00" + this.timestamp;
output = output.substring(output.length() - 2);
} else if (KeyType.text.equals(this.getKey())) {
output += this.text;
} else {
output += super.getType() + ": " + this.sender + ", " + this.text + ", " + this.timestamp;
}
return output;
}
// Test data initializer
public static Messages[] Messagess() {
return new Messages[]{
new Messages("sanjay", 1203, "sample message"),
new Messages("user1", 0304, "another message example"),
new Messages("sampleuser", 0401, "hello world"),
};
}
/* main to test Messages class
*
*/
public static void main(String[] args)
{
// Inheritance Hierarchy
Messages[] objs = Messagess();
// print with title
Messages.setOrder(KeyType.title);
Messages.print(objs);
// print sender only
Messages.setOrder(KeyType.sender);
Messages.print(objs);
}
}
Messages.main(null);
public class CanCode {
// Main driver method
public static void main(String[] args) {
// Creating object of class1
// inside main() method
Page c1 = new Page(10, 15);
// Printing the complex number
System.out.println(c1);
}
}
class Page {
// Attributes of a complex number
private double re, im;
// Constructor to initialize a complex number
// Default
// public Page() {
// this.re = 0;
// this.im = 0;
// }
// Constructor 2: Parameterized
public Page(double re, double im) {
// This keyword refers to
// current complex number
this.re = re;
this.im = im;
}
// Getters
public double getReal() {
return this.re;
}
public double getImaginary() {
return this.im ;
}
// Setters
public void setReal(double re) {
this.re = re;
}
public void setImaginary(double im) {
this.im = im;
}
// Overriding toString() method of String class
@Override
public String toString() {
return this.re + " + " + this.im + "i";
}
}
CanCode.main(null);
public class Excercises {
public String topic;
public int difficulty;
public int successrate;
public Articles(String starttopic, int startsuccessrate, int startdifficulty) {
difficulty = startdifficulty;
topic = starttopic;
successrate = startsuccessrate;
}
public void settopic(String newTopic) {
topic = newTopic;
}
public void setdifficulty(int editDiff) {
difficulty = editDiff;
}
public void successrateUp(int increment) {
successrate += increment;
}
}
public class UserExcercise extends Excercises {
public int userID;
public UserExcercise(int startuserID, String starttopic, int startsuccessrate, int startdifficulty) {
super(starttopic, startsuccessrate, startdifficulty);
userID = startuserID;
}
public void setFact(int newValue) {
seatFact = newValue;
}
}
public class LinkedList<T>
{
private T data;
private LinkedList<T> prevNode, nextNode;
/**
* Constructs a new element
*
* @param data, data of object
* @param node, previous node
*/
public LinkedList(T data, LinkedList<T> node)
{
this.setData(data);
this.setPrevNode(node);
this.setNextNode(null);
}
/**
* Clone an object,
*
* @param node object to clone
*/
public LinkedList(LinkedList<T> node)
{
this.setData(node.data);
this.setPrevNode(node.prevNode);
this.setNextNode(node.nextNode);
}
/**
* Setter for T data in DoubleLinkedNode object
*
* @param data, update data of object
*/
public void setData(T data)
{
this.data = data;
}
/**
* Returns T data for this element
*
* @return data associated with object
*/
public T getData()
{
return this.data;
}
/**
* Setter for prevNode in DoubleLinkedNode object
*
* @param node, prevNode to current Object
*/
public void setPrevNode(LinkedList<T> node)
{
this.prevNode = node;
}
/**
* Setter for nextNode in DoubleLinkedNode object
*
* @param node, nextNode to current Object
*/
public void setNextNode(LinkedList<T> node)
{
this.nextNode = node;
}
/**
* Returns reference to previous object in list
*
* @return the previous object in the list
*/
public LinkedList<T> getPrevious()
{
return this.prevNode;
}
/**
* Returns reference to next object in list
*
* @return the next object in the list
*/
public LinkedList<T> getNext()
{
return this.nextNode;
}
}