Hello I am trying to create a class that called Lootable and create subclasses such as trunk, locker, etc. that will contain items that can be looted, they will inherit their lootability from the Lootable class. I am thinking of using the ArrayList for this. I have thought of different ways to go about this and have written some code but I am having some difficulties. This is for and assignment and my app idea goes somewhat beyond the requirements of the lesson and no I do not expect someone to due this for me I would however like some help understanding the concepts. Here is the code I have so far:

package TaskTwo;

import java.util.ArrayList;

/**
 *
 * @author Steven Richardson
 */
public class Main
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
       Lootable lt = new Lootable();
        Trunk tr = new Trunk();
        Trunk trunkTwo = new Trunk("new stuff worth $");

//        System.out.println(lt.toString());
//        System.out.println(lt.contents);
//        
//        System.out.println(tr.toString());
//        System.out.println(tr.contents);
//        
//        System.out.println(trunkTwo.toString());
//        System.out.println(trunkTwo.contents);

        ArrayList <Lootable> lootContainers = new ArrayList<>(); 
        lootContainers.add(lt);
        lootContainers.add(tr);
        lootContainers.add(trunkTwo);



//        System.out.println(lootContainers.toString());
        System.out.println(lootContainers.get(2).toString());
    }



}





package TaskTwo;

/**
 *
 * @author Steven
 */
public class Lootable {

    public String contents;

    public String getContents() {
        return contents;
    }

    public void setContents(String contents) {
        this.contents = contents;
    }

    public Lootable() {
        this.contents = "gold coin, ruby, crown.";
    }

}




package TaskTwo;

/**
 *
 * @author Steven
 */
public class Trunk extends Lootable
{

   public Trunk() {
    }

    public Trunk(String newContents) {
        this.contents = "gold coin, ruby, crown";
    }

    public String toString(){

    return this.contents.toString();

}
}

This code will not due everything I am wanting. I am showing inheritence though the Trunk class inheriting from Lootable. I need to show overriding can be used to make the trunk not lootable or lock the trunk. I need to show polymorphism in the app. I think I have but I still am not clear on that concept. Any help would be appreciated

Recommended Answers

All 7 Replies

Although your purposes made me think of an interface rather than a class for Lootable. I still try to help you in your own codes.

I also think that there is something does not make sense when you define:

public Trunk(String newContents) 
{
  this.contents = "gold coin, ruby, crown";
}

and call it like this:

Trunk trunkTwo = new Trunk("new stuff worth $");

Because you haven't used newContents to initialize any thing :(

OK, so you want a class Lootable as a super class, I would do it like this (nothing new, just revised from your own codes):

public class Lootable 
{
  public String contents;
  public String getContents() 
  {
    return contents;
  }

  public void setContents(String contents) 
  {
    this.contents = contents;
  }

  public Lootable() 
  {
    // do nothing here
  }
}



public class Trunk extends Lootable
{
    public Trunk()
    {
      // also do nothing here because I dont know what to do here
    }

    public Trunk(String newContents)
    {
        this.contents = newContents;
    }

    public String toString()
    {
        return this.contents.toString(); // toString() here also does not required, because String can return itself a String :)
    }

    public String getContent() // this is new method
    {
        return this.contents ;
    }

    public String setContent(String newContents)
    {
        this.contents = newContents ;
    }
}

Having these 2 classes, you can declare in main:
Trunk trunkTwo = new Trunk("new stuff worth $");

and to get the "things" in the trunkTwo, your can:
String var = trungTwo.getContent()

Moreover if you still want to have another class called Locker, just extends the Trunk class, add new variable boolean isLocked and check isLocked when you want to getContent():

I doubt this is an assignment from your school, and somehow you were confused by the Teacher's ideas, if not, never mind.

Hope this help :)

commented: Thank you you are correct I did not have and interface. I have rewritten the code to use and interface just to check and see if the various containers are lootable. I could still use some help with adding some overriding to my app. I will post the code be +0

Didn't say that I am still confused with your codes and ideas even after posting. There are many others can help you on this

Hello Tu Dinh,
I am confused by my own code as well. I was trying different ideas. Below is the task from my course:

A. Develop a working application that implements an interface. The interface can be one that you have created, or you can implement one provided by the Java API.

Some interfaces provided by the Java API to consider for your project are Runnable, Maps, Sets, or Lists, if you choose not to create one of your own.

B. Present your application to a small group and do the following
1. Explain how you used the Interface.
2. Explain how overriding through an Interface was used.
3. Lead a discussion with your attendees on the differences between overriding and overloading.
a. Discuss where polymorphism was used in the application.
b. Identify where you used overriding or overloading in your application. Thanks for the help.

According to the Exam, I think my codes just lacked overriding and overloading methods.

So you still need my help or not? I think your ideas about the trunk and locker is great to apply those theories.

commented: Yes I still could use some help +0

A. Develop a working application that implements an interface.

So far you have no interface in your code. If only to meet the requirements of the exercise you could make Lootable an interface - it already sounds more like an interface (it's an adjective) not a class (its's not a noun).

If you look at primary sources, like Oracle's own documentation, you will see that it refers to "implementing" an interface by implementing its methods. Your tutor's use of "overriding" in point 2 seems to be a mistake; I would assume they mean "implement" - but as always if your tutor is wrong it's safer to stick with their version at least until yur paper has been marked!

commented: Thank you you are correct I did not have and interface. I have rewritten the code to use and interface just to check and see if the various containers are lootable. I could still use some help with adding some overriding to my app. I will post the code be +0
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package task_two;

import task_two.containers.Trunk;
import task_two.containers.Locker;
import task_two.containers.BackPack;
import java.util.ArrayList;

/**
 *
 * @author Steven
 */
public class Task_Two
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        Trunk t1 = new Trunk(2); // not lootable
        Trunk t2 = new Trunk(1);
        Trunk t3 = new Trunk(2001); // not lootable

        Locker l1 = new Locker(258); // wrong combination not lootable
        Locker l2 = new Locker(369); // correct combination lootable
        Locker l3 = new Locker(123); // BZZZ wrong answer hnot lootable

        BackPack bp1 = new BackPack();
        BackPack bp2 = new BackPack();
        BackPack bp3 = new BackPack();

//        ArrayList<Object> lootContainers = new ArrayList<>();
        ArrayList<Lootable> lootContainers = new ArrayList<>();

        lootContainers.add(t1);
        lootContainers.add(t2);
        lootContainers.add(t3);
        lootContainers.add(l1);
        lootContainers.add(l2);
        lootContainers.add(l3);
        lootContainers.add(bp1);
        lootContainers.add(bp2);
        lootContainers.add(bp3);

//        for (int i = 0; i < lootContainers.size(); i++) {
//            if (lootContainers.get(i) instanceof Trunk) {
//                Trunk t = (Trunk) lootContainers.get(i);
//                if (t.isLootable()) {
//                    t.loot();
//                }
//            } else {
//                if (lootContainers.get(i) instanceof Locker) {
//                    Locker l = (Locker) lootContainers.get(i);
//                    if (l.isLootable()) {
//                        l.loot();
//                    }
//                } else {
//                    if (lootContainers.get(i) instanceof BackPack) {
//                        BackPack bp = (BackPack) lootContainers.get(i);
//                        if (bp.isLootable()) {
//                            bp.loot();
//                        } else {
//                            System.out.println("The Container is not lootable");
//                        }
//
//                    }
//                }
//            }
//        }
        System.out.println();
//        for (int e = 0; e < lootContainers.size(); e++) {
//            if (lootContainers.get(e) instanceof Lootable) {
//                Lootable item = (Lootable) lootContainers.get(e);
//                if (item.isLootable()) {
//                    item.loot();
//                }
//            }
//
//        }

          for (int e = 0; e < lootContainers.size(); e++)
          {
              if(lootContainers.get(e).isLootable())
                  lootContainers.get(e).loot();
          }
    }
}





package task_two;

/**
 *
 * @author Steven
 */
public interface Lootable
{
    public boolean isLootable();
    public void loot();

}




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package task_two.containers;

import task_two.Lootable;

/**
 *
 * @author Steven
 */
public class Trunk implements Lootable 
{
    private int locked;



    public Trunk(int l)
    {
      locked = l;
    }



    @Override
        public boolean isLootable()
    {
        if (locked !=1)
            System.out.println("This Trunk is locked");
        return locked == 1;
    }
    @Override
        public void loot()
        {
            System.out.println("This trunk is open");
        }

          public Trunk (String Contents)
          {
              this.toString();
          }
}




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package task_two.containers;

import task_two.Lootable;

/**
 *
 * @author Steven
 */
public class Locker implements Lootable
{

    private int locked;

    public Locker()
    {

    }

    public Locker(int l)
    {
        locked = l;
    }

    @Override
    public boolean isLootable()
    {
        if (locked != 369) {   // Combination is 369
            System.out.println("This locker is Locked");
        }
        return locked == 369;
    }

    @Override
    public void loot()
    {
        System.out.println("This locker is unlocked, it's all mine!");
    }

}




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package task_two.containers;

import task_two.Lootable;

/**
 *
 * @author Steven
 */
public class BackPack extends Container implements Lootable
{

    @Override
    public boolean isLootable()
    {
        return true;
    }

    @Override
    public void loot()
    {
        System.out.println("Easy Money!");
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package task_two.containers;

/**
 *
 * @author Steven
 */
public class Container
{
    private String itemName;
      private int qty = 0;
    public Container()
    {        

    }
    public Container(String ItemName, int QTY)
    {
        itemName = ItemName;
        qty = QTY;
    }

    /**
     * @return the itemName
     */
    public String getItemName()
    {
        return itemName;
    }

    /**
     * @return the quantity
     */
    public int getQty()
    {
        return qty;
    }

    /**
     * @param itemName the itemName to set
     */
    public void setItemName(String itemName)
    {
        this.itemName = itemName;
    }

    /**
     * @param qty the quantity to set
     */
    public void setQty(int qty)
    {
        this.qty = qty;
    }
}

Tu, and James thanks for the point in the right direction I finished it last night. Now all that's left is to present it to my classmates and expain polymorphism and overriding which I now get.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.