What is container class in java

Container class java

The problem can be summarized as follows: I have a base abstract class Player and two concrete subclasses, Signaller and Receiver: Now I need classes which represent collections of Signallers and Receivers and I am constrained to using arrays to store them. I’m confused because everything that I read says container classes are things like java.util.

Container class java

I’ve got a problem and I don’t understand how to solve it.

So I have 3 classes: first one is main, second one is book and the third one is a shelf which contains some books.

public class Book < private String title; private int year; private String edition; public Book(String title, int year, String edition) < this.title= title; this.year= year; this.edition = edition; >public Book(Book l)
public static void main(String[] args) < Book one = new Book("Title1", first, 2014, "Edition1"); Book two = new Book("Title2", second, 2013, "Edition2"); Book three= new Book("Title3", third, 2015, "Edition3"); Book four = new Book("Title4", fourth, 2015, "Edition4"); Book[] v = new Book[3]; v[0] = one; v[1] = two; v[2] = three; Shelf shelf= new Shelf(); try< shelf.append(four); >(catch myException e)
public class Shelf < private Book[] v; public Shelf() < v = new Book[3]; >public void append (Book x) throws myException < if(x != null && v != null) < Book[] vAppend = new Book[v.length+1]; for(int i=0; ivAppend[v.length] = new Book(x); v = vAppend; > throw new myException("Null"); > 

How can I do the append method in shelf class? Obviously it give me NullPointerException because the array i’m using is null, but I don’t know how to manage it.

Читайте также:  Моделирование солнечной системы python

One more question, how do I insert Book objects in Shelf class?

At first in the constructor I wrote » this.v = v » so when I declared a shelf object I passed its the v array in main class, which is not null, it worked but the exercise said not to do like this.

P.S. I have to do this exercise with normal array, I mean without arrayList, I still have to learn it.

At the moment you are expanding and copying every time a new book is inserted. This is not really necessary. Better is to have an initial capacity that is expanded only when the array is filled.

private static final int INCREMENT = 100; private int capacity = 0; private int size = 0; private Book[] books = <>; public void append(Book book) < assert book != null; expandIfFull(); books[size++] = book; >private void expandIfFull() < assert size > 

This has a performance advantage but it also seems to me to make your intent clearer by splitting the expansion code from the appending code. Having a separate method means that it can be called in your insert method as well.

You could reduce the expansion code using Arrays methods but I’m assuming the fact that you can’t use Java collections implies that you need to do it manually.

You also asked about how to insert; I’ve added an example method for that. The key thing to remember is you need to shift the books after the insert position from the end first. Otherwise you’ll just end up copying the same book through the rest of the array.

public void insert(int index, Book book) < assert index < size; expandIfFull(); for (int i = size; i >index; i--) books[i] = books[i - 1]; books[index] = book; size++; > 

The issue is in your Shelf constructor.

You are initializing the array to be size 3, but you never fill it with books in the constructor. So once you do new Shelf() It has a Book array of size 3 with 3 null values in it.

So, you get a NPE because your append(Book x) method assumes that there are no null values in your Book array.

public void append(Book x) < Book[] vAppend = new Book[v.length + 1]; for (int i = 0; i < v.length; i++) < // ERROR: v.length is 3 but // v[0], v[1], and v[2] will be null initially!! vAppend[i] = new Book(v[i]); >vAppend[v.length] = new Book(x); v = vAppend; > 

To fix the issue: The simplest way is to change your constructor to make an array of size 0. Then the rest of your code will handle it.

You could use something like this:

public class Shelf < private Book[] v; public Shelf() < v = new Book[0]; >public Shelf append (Book x) throws myException < if(x != null) < v = Arrays.copyOf(v, v.length + 1); v[v.length] = new Book(x); return this; >throw new myException("Null"); > 

Packages In Java, Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are used for: Preventing naming conflicts. For example there can be two classes with name Employee in two packages, college.staff.cse.Employee and college.staff.ee.Employee. Making …

Creating a Java container class

I have been given an java assignment in school that requires me to create a StockQuote class. This would normally be easy, however the teacher has referred to it as a simple container class. I’m confused because everything that I read says container classes are things like java.util.Vector, java.util.Hashtable, and java.util.HashSet. I get the feeling he is using this term to mean something else, perhaps even just to mean a strightforward StockQuote class. I tried emailing him but he hasn’t responded and I’d like to get a jump on the assignment. Here is the description from the assignment:

«A StockQuote class or interface. This a simple container class. Typically you would not use an interface for container classes, but you could. One rule for when to use an interface or not is to decided if there ever possibly could be more than one implementation of the class. If more than one implementation is possible, then using an interface definitely makes sense. In the case of simple container classes like this one, there probably will only be one implementation»

Any help or nudge in the right direction would be great. Thanks

In your case This a simple container class. == This a simple class. .

In general your class may have some fields of other types, like String, Collections, etc. If so, you would say I have a container class because it contains/stores some data. Interfaces don’t have fields, so they are not containers.

Container Class | Microsoft Docs, command () Get the commands to execute within the container instance in exec form. environment Variables () Get the environment variables to set in the container instance. image () Get the name of the image used to create the container instance. instance View () Get the instance view of the container instance. Only valid in …

What is the importance of the Container class in Java?

Container

  • A Container class can be described as a special component that can hold the gathering of the components.
  • There are two types of Swing Containers, they are top-level containers and low-level containers.
  • Top-level containers are heavyweight containers such as JFrame , JApplet , JWindow , and JDialog .
  • Low-Level containers are lightweight containers such as JPanel .
  • The most commonly used containers are JFrame , JPanel and JWindow .
  • The important methods of the Container class are add() , invalidate() and validate() .

Example

import java.awt.*; import javax.swing.*; public class ContainerTest extends JFrame < // top-level container JPanel panel; // low-level container JTextField field; JButton btn; public ContainerTest() < setTitle("Container Test"); panel = new JPanel(); field = new JTextField(20); panel.add(field); btn = new JButton("Submit"); panel.add(btn); add(panel, BorderLayout.CENTER); setSize(350, 275); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); >public static void main(String args[]) < new ContainerTest(); >>

Output

Java AWT Tutorial, There are four types of containers in Java AWT: Window Panel Frame Dialog Window The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window. We need to create an instance of Window class to create this container. Panel

Java Generic Container Class

I’m working on a evolutionary simulation model implemented in Java and ran into a key object-orientation design issue which I can’t seem to work out. The problem can be summarized as follows:

I have a base abstract class Player and two concrete subclasses, Signaller and Receiver:

abstract class Player < Strategy[] strategies; double fitness; . >class Signaller extends Player < double quality; . >class Receiver extends Player

Now I need classes which represent collections of Signallers and Receivers and I am constrained to using arrays to store them. There are methods common to both population types, but also specific methods for a signaller populations or for a receiver population.

Conceptually , I would need something like this:

abstract class Population < Player[] members; void mixUpPopulation() Strategy[] getMeanStrategies() double getMeanFitness() . > class SignallerPopulation extends Population < Signaller[] members; . >class ReceiverPopulation extends Population < Receiver[] members; double[] getChannelPreferences() . > 

I have thought of two basic ways of achieving this:

  1. Have the class hierarchy as described above.
    Problem : How can the Player[] in the superclass and also the Signaller[] or Receiver[] in the subclasses refer to the same collection of objects?
  2. Make the base class generic:

Problem : How do I implement the methods specific to each of the population types?

I would appreciate your insights into those problems or maybe suggestions of other ways of tackling the problem.

You can use the design 1 as in your question, but instead of storing an array in the abstract base class you add an abstract protected method (e.g. getmembers()) that will be implemented in the subclasses to return the actual array as an array of Players.

Alternatively, you can make the abstract base class generic, and derive the subclasses with the appropriate types:

abstract class Population  < T[] members; void mixUpPopulation() Strategy[] getMeanStrategies() double getMeanFitness() . > class SignallerPopulation extends Population  < public SignallerPopulation(int popSize) < members = new Signaller[popSize]; >. > class ReceiverPopulation extends Population  < public ReceiverPopulation(int popSize) < members = new Receiver[popSize]; >double[] getChannelPreferences() . > 

Remove the members from Population and add an abstract getter-method for members to it ( public abstract Player getMember(int i) and public abstract int getNumPlayers() or something similar). Subclasses are required to implement the getter. This way you will still have access to the Player part of XYPopulation members in Population .

I went with the design suggested by @Medo42 (his first option) and @LumpN. The only cast required was when setting the array, but that wasn’t problematic. I am giving the outline of the code here, maybe someone will find it helpful.

abstract class Population < protected abstract Player[] getMembers(); protected abstract void setMembers(Player[] members); void mixUpPopulation() Strategy[] getMeanStrategies() double getMeanFitness() . > class SignallerPopulation extends Population < Signaller[] members; protected Player[] getMembers() < return this.members; >protected void setMembers(Player[] members) < this.members = (Signaller[]) members; //required cast >. > class ReceiverPopulation extends Population < Receiver[] members; protected Player[] getMembers() < return this.members; >protected void setMembers(Player[] members) < this.members = (Receiver[]) members; //required cast >double[] getChannelPreferences() . > 

Java Adapter Classes, It provides a way to include related patterns in the class. It provides a pluggable kit for developing an application. It increases the reusability of the class. The adapter classes are found in java.awt.event, java.awt.dnd and javax.swing.event packages. The Adapter classes with their corresponding listener interfaces are given below.

Источник

Оцените статью