Inheritance and Polymorphism

I'm having trouble with the program I wrote. Most notably with the compareTo() method I have to implement. Also when I try and call a superclass. I comments out problems and concerns along the way. Any help would be great!
[CODE]
import javax.swing.JOptionPane;
public class A2Q1 {
public static void main (String args[]){

// An array of Threats
Threat[] threats = new Threat[5];

System.out.println("Threat assessment : ");

// Construct objects
threats[0] = new Phishing("Credit card information", "CanadaTrust");
threats[1] = new Hoax("Wanna Win an Ipod?");
threats[2] = new Virus("Trojan", "e-mail reproduction");
threats[3] = new Spyware("WeatherFinder");
threats[4] = new Spyware("WeatherFinder");

// Processing

// This one should be negative
System.out.println("0 and 1: " +
threats[0].compareTo(threats[1]));
// This one should be positive
System.out.println("2 and 1: " +
threats[2].compareTo(threats[1]));
// This one should be negative
System.out.println("2 and 3: " +
threats[2].compareTo(threats[3]));
// This one should be zero
System.out.println("3 and 4: " +
threats[3].compareTo(threats[4]));

((Malware)threats[2]).increaseSeverity();

// This one should be positive
// (since 2 now has greater severity than 3)
System.out.println("2 and 3: " +
threats[2].compareTo(threats[3]));

// Output
// Polymorphism at work!
for (int i = 0; i < threats.length; i++) {
System.out.println(threats[i].toString());
}
}
}
abstract class Threat{
public Threat(String new_name){
name = new_name;
}
public String getName(){
return name;
}
public String toString(){
return "Threat : " + name; //format properly
}
public int compareTo(Object other){ //only be overidden in malware class *Causes problems when compiled*

final int less = -1;
final int equals = 0;
final int more = 1;
int compare;

if(this == other){
return equals;
}

compare = this.name.compareTo(other.name); //cannot find symbol ERROR!

if (compare != equals){
return compare;
}
}
private String name;
}
class Phishing extends Threat{
public Phishing(String new_name, String new_target){
super(new_name);
target = new_target;
}
public String toString(){
return super.toString() + ", " + target;
}
public String getTarget(){
return target;
}
private String target;
}
abstract class Malware extends Threat{
public Malware(String new_name, int new_severity){
super(new_name);
severity = new_severity;
}
public int getSeverity(){
return severity;
}
public void increaseSeverity(){
severity++;
}
public String toString(){
return super.toString() + ", " + severity;
}
public int compareTo(Object other){//Comparison, if both are malware compares severity, if severity matches compares names...

//copied one from threat trying to find out where to begin..?
final int less = -1;
final int equals = 0;
final int more = 1;
int compare;

//need a base case incase they do not equal

if(this == other){ //if both are malware, compare severity
//must compare severity here...
compare = this.severity.compareTo(other.severity);

if(compare !=equals){ //severity doesn't match
return compare
}

return this.name.compareTo(other.name);
}

return compare
}

private int severity = 1; //supposed to be initialized to 1
}
class Virus extends Malware{

//public Virus(int new_severity, String new_damage){ *what i had before*
public Virus(String new_name, String new_damage){
//super(new_damage, new_severity); *what i had before*
super(new_name, 1); //where to get the integer from for super class? set it as any value just to compile program
damage = new_damage;
}
public String toString(){
return super.toString() + ", " + damage;
}
public String getDamage(){
return damage;
}
private String damage;
}
class Spyware extends Malware{
public Spyware(String new_name){
super(new_name, 1); //where to get the integer from for super class? set it as any value just to compile program
}
public String toString(){
return super.toString();
}
}
class Hoax extends Threat{

public Hoax(String new_name){
super(new_name);
}

public String toString(){
return super.toString();
}
}
[/CODE]

Comments

  • : I'm having trouble with the program I wrote. Most notably with the compareTo() method I have to implement. Also when I try and call a superclass. I comments out problems and concerns along the way. Any help would be great!
    : [CODE]
    : import javax.swing.JOptionPane;
    : public class A2Q1 {
    : public static void main (String args[]){
    :
    : // An array of Threats
    : Threat[] threats = new Threat[5];
    :
    : System.out.println("Threat assessment : ");
    :
    : // Construct objects
    : threats[0] = new Phishing("Credit card information", "CanadaTrust");
    : threats[1] = new Hoax("Wanna Win an Ipod?");
    : threats[2] = new Virus("Trojan", "e-mail reproduction");
    : threats[3] = new Spyware("WeatherFinder");
    : threats[4] = new Spyware("WeatherFinder");
    :
    : // Processing
    :
    : // This one should be negative
    : System.out.println("0 and 1: " +
    : threats[0].compareTo(threats[1]));
    : // This one should be positive
    : System.out.println("2 and 1: " +
    : threats[2].compareTo(threats[1]));
    : // This one should be negative
    : System.out.println("2 and 3: " +
    : threats[2].compareTo(threats[3]));
    : // This one should be zero
    : System.out.println("3 and 4: " +
    : threats[3].compareTo(threats[4]));
    :
    : ((Malware)threats[2]).increaseSeverity();
    :
    : // This one should be positive
    : // (since 2 now has greater severity than 3)
    : System.out.println("2 and 3: " +
    : threats[2].compareTo(threats[3]));
    :
    : // Output
    : // Polymorphism at work!
    : for (int i = 0; i < threats.length; i++) {
    : System.out.println(threats[i].toString());
    : }
    : }
    : }
    : abstract class Threat{
    : public Threat(String new_name){
    : name = new_name;
    : }
    : public String getName(){
    : return name;
    : }
    : public String toString(){
    : return "Threat : " + name; //format properly
    : }
    : public int compareTo(Object other){ //only be overidden in malware class *Causes problems when compiled*
    :
    : final int less = -1;
    : final int equals = 0;
    : final int more = 1;
    : int compare;
    :
    : if(this == other){
    : return equals;
    : }
    :
    : compare = this.name.compareTo(other.name); //cannot find symbol ERROR!
    [red] // use getName() instead of name. Name is private[/red]
    :
    : if (compare != equals){
    : return compare;
    : }
    : }
    : private String name;
    : }
    : class Phishing extends Threat{
    : public Phishing(String new_name, String new_target){
    : super(new_name);
    : target = new_target;
    : }
    : public String toString(){
    : return super.toString() + ", " + target;
    : }
    : public String getTarget(){
    : return target;
    : }
    : private String target;
    : }
    : abstract class Malware extends Threat{
    : public Malware(String new_name, int new_severity){
    : super(new_name);
    : severity = new_severity;
    : }
    : public int getSeverity(){
    : return severity;
    : }
    : public void increaseSeverity(){
    : severity++;
    : }
    : public String toString(){
    : return super.toString() + ", " + severity;
    : }
    : public int compareTo(Object other){//Comparison, if both are malware compares severity, if severity matches compares names...
    :
    : //copied one from threat trying to find out where to begin..?
    : final int less = -1;
    : final int equals = 0;
    : final int more = 1;
    : int compare;
    :
    : //need a base case incase they do not equal
    :
    : if(this == other){ //if both are malware, compare severity
    [red]// this if checks if both are the same instance, not if both are same class. This is only true for following call: v.compareTo(v);
    // Solution: use instanceof
    [/red]
    : //must compare severity here...
    : compare = this.severity.compareTo(other.severity);
    :
    : if(compare !=equals){ //severity doesn't match
    : return compare
    : }
    :
    : return this.name.compareTo(other.name);
    : }
    :
    : return compare
    : }
    :
    : private int severity = 1; //supposed to be initialized to 1
    : }
    : class Virus extends Malware{
    :
    : //public Virus(int new_severity, String new_damage){ *what i had before*
    : public Virus(String new_name, String new_damage){
    : //super(new_damage, new_severity); *what i had before*
    : super(new_name, 1); //where to get the integer from for super class? set it as any value just to compile program
    [red] // check the order of the parameters[/red]
    : damage = new_damage;
    : }
    : public String toString(){
    : return super.toString() + ", " + damage;
    : }
    : public String getDamage(){
    : return damage;
    : }
    : private String damage;
    : }
    : class Spyware extends Malware{
    : public Spyware(String new_name){
    : super(new_name, 1); //where to get the integer from for super class? set it as any value just to compile program
    [red] // check the order of the parameters[/red]
    : }
    : public String toString(){
    : return super.toString();
    : }
    : }
    : class Hoax extends Threat{
    :
    : public Hoax(String new_name){
    : super(new_name);
    : }
    :
    : public String toString(){
    : return super.toString();
    : }
    : }
    : [/CODE]
    :
    See red above.
  • My biggest problem at the moment is the overriding compareTo() method inside Malware class. I need an if statement that compares severity if both objects are Malware. Then if severity matches, compares by name....returns either compairson if they do not match. Here's what i've tried (among many other things). I know this code isn't even right but i've tried a lot of diffrent things with no luck.

    [CODE]
    if(other instanceof Malware){ //both malware objects

    return severity.compareTo(((Malware) other).severity); //compare severity (but int cannot be deferenced, and other ways I try it I get a Casting Exception)
    }

    return name.compareTo(((Threat) other).name); //returns name comparison
    }
    [/CODE]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories