Interface / Abstract Classes And Loading Array

Oct 20, 2014

I am new to Java, and last week had an assignment to create a shopping list. I made it so that I have one class use a ProductData class to load an array of objects (description, price, priority). This week I need to take that program and change it so that it includes an Interface and Abstract Class. I need to also split one class up into at least 2 others.

I am having trouble getting my thoughts together and figuring out what to put in the interface and what to put in the abstract class. I'm thinking that it might be best to split up the ProductData class up into 3 different classes: description, price, and priority. Then have an interface with a print method. Each of those 3 classes will implement the interface.

As for the abstract class, have the price and priority extend the abstract class. The abstract class will be at the same level as the interface and contain the set and get methods. Right now they are of 2 different data types: int, double. Should I make both of them Double, and then use a method to change the priority to an int?

Should price and priority inherit from description, or should they all be at the same level? I am thinking that they should be at the same level because they all describe the item in the array.

My most confusing part is that I have no clue at all on how I can load that array when each object is split up in a different class. My professor went over ArayLists last week, and we can now use them if we want, but the assignment doesn't explicitly say that we should change it to an Array List. Where does the constructor for the ProductData() go? Do I split it up into 3 different constructors?

View Replies


ADVERTISEMENT

Interface And Abstract Classes In Java?

Jun 4, 2014

why don't I define my methods in a class, rather than going a level up and declaring it first in an abstract class/interface? If the point is to have different implementations for different needs, then we have the option to override the methods.

View Replies View Related

Loading Interface Array Of Objects?

Oct 27, 2014

I have an assignment where I need to add an interface to an already-created program. I have an array of objects, where each object has a name, price, and priority.

In my original program (which worked), I had all of the objects in 1 class. The professor said that I should split up the name/price/priority into 3 different classes. So what I have is an array that belongs to an interface, and name/price/priority implements. But I am having trouble loading the data into the array (from another class) once I am done with loading name.

Here is what I have so far.

public class Main {
public static void main (String[] args){
Interface[] arr = new Interface[7];
Scanner keyboard = new Scanner(System.in);
System.out.println

[code]....

Line 18 in the ItemName class is giving me an error, and I know it is because arr is of type Interface, and I am trying to assign is a String. But I don't know how to do this. In my original program I was able to do arr[x].getName(); but when I do that in Main, I get errors.

how to assign to an interface array from multiple classes.

View Replies View Related

What Are Benefits Of Using Interface Plus Abstract Class Over Just Abstract Class

Oct 8, 2014

What are the benefits of using an Interface plus an abstract class, over just an abstract class?

View Replies View Related

When To Use Interface And Abstract Class

Apr 17, 2014

I know whats the interfaces and abstract class and also know that difference between interface and abstract class,but here my doubt is eventhough abstract class more advantage than the interface,then why should we use interfaces and when?

View Replies View Related

Difference Between Abstract Class And Interface?

May 18, 2011

Difference between Abstract class and Interface??

View Replies View Related

Interface Inside Abstract Class

Jul 8, 2015

What this interface inside that abstract class does. Looking for some examples to how can i use it .... 

public abstract class Expression {
  public abstract String toString();
  public abstract SimpleExpression evaluate();
  public void show() {
  System.out.println(this + " = " + evaluate());

[Code] ....

View Replies View Related

What Programs Use Abstract Classes

Sep 15, 2014

What programs use abstract classes?

View Replies View Related

Abstract Classes And Interfaces

Mar 29, 2014

I did research again....

interface:
methods - abstract, default, static ONLY(abstract methods have no body, while static and defaults do, right?)
fields - public, static, final ONLY
abstract class: a normal class, but has at least one abstract method
methods - all
i.e., static, non-static, abstract (can it have a default method?)
fields - all
i.e., public, protected, private / final, non-final / static, non-static

View Replies View Related

How Does Inheritance Work With Abstract Classes

Nov 19, 2014

I am writing small pieces of code to make sure I understand Java basics and I have the following.

package teams1;
public abstract class Team1{
private String sport = new String();
public abstract String getSport();
public abstract void setSport();
}
import teams1.*;

[Code] .....

It doesn't compile because sport is private in the super class, but I thought FootballTeam1 would inherit it's own copy of sport because it is extending Team1.

View Replies View Related

Abstract Classes And Access Modifiers

Jan 21, 2015

I'm having difficulty understanding this small piece of code:

package food;
public abstract class Fruit {
private String color;
private String tasteType;
public void setColor(String color) {
this.color = color;
}
public abstract void setTasteType(String taste);
}

The above is an abstract class which describes the basic structure that every fruit should "extend".

The below is a concrete subclass of the Fruit class called Apple.

import food.Fruit;
class Apple extends Fruit {
public void setTasteType(String taste) {
tasteType = taste;
}
}

Also do note that the two pieces of code are in different packages!

Upon compiling the Apple class I get the following error:
Apple.java:4: error: tasteType has private access in Fruit
tasteType = taste;
^

What I don't understand is this: I've given a non-abstract implementation to the "setTasteType" method in the Apple class and clearly setTasteType should have the authority to modify the private instance variables of Fruit. But it turns out I'm wrong.

View Replies View Related

Loading Classes At Program Startup?

Nov 29, 2014

At startup, the class containing your main method is loaded. It loads all classes that it needs. Each of those loaded classes loads the classes that it needs, and so on. That can take a long time for a big application, frustrating the user. You can give users of your program the illusion of a faster start with the following trick. Make sure that the class containing the main method does not explicitly refer to other classes. In it, display a splash screen. Then manually force the loading of other classes by calling Class.forName.

I'm not sure if I got this right - the tip amounts to suggesting to load all the classes altogether in the main method while displaying the splash screen?

Second, how can the main method not refer explicitly to other classes? It has to create some objects after all...

View Replies View Related

Error Using Abstract Classes - Cannot Find Symbol

Feb 2, 2014

I am stuck. It seems like I have done everything by the book but I keep getting the same error: cannot find symbol. The error is specifically addressing lines 9, 10, and 11 in the Alien class file. All that is supposed to happen is an output of information for the two types of aliens.

Java Code:

import javax.swing.*;
public class CreateAliens
{
public static void main (String[] args)
{
Martian aMartian = new Martian();
Jupiterian aJupiterian = new Jupiterian();
JOptionPane.showMessageDialog
(null, "

[Code] ....

View Replies View Related

Why Super Classes Always Declared As Interfaces And Not Abstract Class

Dec 1, 2014

While reading the design patter book, i got one doubt ,There is a List an interface having sub classes ArrayList, LinkedList etc.,

Q1) My question is Why they declared the List as interface rather than Abstract class?

Q2) i read some site -

List l = new ArrayList(); Why it is GOOD line?
ArrayList l = new ArrayList() ; Why it is BAD line?

Answer required with detailed information for Q1 and Q2.

View Replies View Related

Using Comparable Interface Between Two Classes

Feb 13, 2014

I have the following code that will make linked list and order its elements using self referential objects. but i have the following error:
incompatible types

required: ListNode<T#2>
found: ListNode<T#1>
where T#1,T#2 are type-variables:
T#1 extends Comparable declared in method <T#1>insertInOrder(T#1)
T#2 extends Comparable declared in class OrderedList

import java.util.*;
public class ListNode<T> {
ListNode<T> nextNode;
T data;
public ListNode(T item)
{
this(item, null);

[code]...

View Replies View Related

ArrayList Of Interface - Does It Rip Away Data From Child Classes?

Nov 29, 2014

If I lets say have an interface Animal, and I create a lot of classes with a different animal name that implement the interface Animal. Then I create an ArrayList of Animal. Then I would put in lets say Dog class into the ArrayList, which has custom methods and data that the Animal Interface doesn't have, is this data ripped away except for the methods that are put in the Animal interface? So if I would cast the Animal back to Dog, would it retain all the data that existed before it was placed in the ArrayList?

View Replies View Related

Additional Methods From Specific Classes That Implement Interface

Jan 8, 2014

I am writing a game in Java for Android (although my question isn't Android or Game Dev specific).

I have a SceneManager class and a Scene interface and then various other classes that implement the Scene interface (Code at the end of this post).

Basically, in my MainGame class (which also implements the Scene Interface for Touch Event capturing purposes) I hold the bulk of my game code. Methods in this class are then called from my Level classes. (most of these are needed in all levels so it makes sense to hold them here and call them from the levels to eliminate unnecessary code duplication)

So, I have Level1, Level2......... Level20 classes which all implement Scene.

Now, the problem comes because in only 2 of my Levels something can happen (that can't in the other 18) and I need to run a response method in these 2 levels (the method isn't exactly the same, the response to this event happening is different for both levels).

To run common methods from my classes, I use my Scene Manager like this:

SceneManager.getInstance().getCurrentScene().updateLogic();
SceneManager.getInstance().getCurrentScene().render();

(The above is from my gameloop) - So it will run the updateLogic(); and render(); methods from whichever is the current scene (Level).

Scene is changed like so:

SceneManager.getInstance().setCurrentScene(LevelX);

This works great as all Level's have an updateLogic(); and render(); method.

So from my mainGame class, I am doing something like : (pseudo code)

public void checkIfSomethingHappened(){
if (something happens){
if (currentLevel==5){
Level5.response();}

[Code]....

The above would be called from my 2 level classes. So something like:

MainGame.checkIfSomethingHappened(); //Called in addition to the normal methods that make up that level

I don't really want to have this (second) 'if' statement here in the middle of my performance critical game loop.

What I'm after is something like this:

if (something happens){
SceneManager.getInstance().getCurrentScene().response();
}

However, this would require me to put stubs in the other 18 classes.

I'm thinking there must be a way to do this as the SceneManager already knows the current scene so it seems a waste checking it again via an if (or switch) statement. What is the best way to do this without having to put stubs into classes that don't require this method?

View Replies View Related

Swing/AWT/SWT :: GUI - Create User Interface That Interacts With Setters And Getters Of Some Classes

Apr 14, 2014

I am new a creating GUIs and am not quite sure how to correctly make one. I have done the inheritance parts, and created two extra appliances: a washer and dryer. Now Creating the GUI ....

Here are the instructions to my project.

Introduction to GUIs (+ some inheritance)

For this assignment, you are going to create a user interface that interacts with the setters and getters of some classes that you will create.

First, create an abstract class called Appliance. This abstract class should have two attributes (dealing with household appliances) and two abstract methods called turnOn() and turnOff(). These methods should return void.

Then, create two subclasses of Appliance that represent household appliances (like a Refrigerator or Stove ((don't use those!))). These subclasses should have two attributes that are specific to the various appliance. Each subclass should implement the turnOn() and turnOff() methods. These methods should print to the command line some information about the appliance as it turns on and off.

Now, the fun part! Create a GUI interface!

Your window should have two panels: one for each appliance subclass. Each panel should have 4 textboxes (with appropriate labels) to receive/display information that correspond to the 4 attributes (2 from Appliance and 2 from the subclass) for each subclass.You also need 2 buttons on each panel: A Get button and a Set button.

When the Get button is pressed, the text boxes should be filled with the information from the instantiated object of the appropriate subclass. When the Set button is pressed, the object should then contain the information contained that the user has altered.

In your main method, you should create an object of each subclass, and prefill it with information (either using the constructor or the setters), then display your GUI. You should now be able to get and set the information for your objects from the GUI.

At least one of your attributes for each subclass should be numeric

Note that you will need to handle incorrectly formatted input (You can use exception handling to do this if you want to. Wrapper classes also will work)

If there is text in the boxes when the "Get" button is pressed, it should be overwritten by what is in the object. Remember that these two panels should both be on screen at the same time.

You don't need 2 different windows, one window: 2 panels.

View Replies View Related

Custom Exception Classes And Array

Jul 20, 2014

I've been trying to work with custom Exception classes, but I keep running into what I think is an array error. It's in a very monolithic format because I was just trying to bang it out and get it done. Anyway, my issues is I am trying to compare values in the array to the minimum and maximum possible scores for a student (0 and 100) but I have totally forgotten how to do it.

Here is the code, the offending bit is at the very bottom:

package org.CIS407.Lab6;
import java.util.*;
public class TestScore {
public static void main(String[] args) throws ScoreException {
Scanner scan = new Scanner(System.in);
int sz; //holds scanner values
int[] studentArray;

[Code] .....

Right now I'm getting an error when I go to enter the very last student score. It throws an exception.

Here is a sample output of what I'm getting:

Enter number of students
2
Array created successfully. Enter student ID's into array
1
2
Enter number of scores
2
Array created successfully. Enter student scores into the array
50
32

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at org.CIS407.Lab6.TestScore.main(TestScore.java:64)

View Replies View Related

Interface Extends More Than One Interface

Jun 9, 2014

Below code gets printed as output?

public interface I1 {
public void method();
}
public interface I2 {
public void method();
}
public interface I3 extends I2, I1 {

[Code] ....

View Replies View Related

What Is Abstract Class

Jun 1, 2014

I am just started to learn java and i am facing trouble learning abstract class.

View Replies View Related

Images Not Loading

Nov 15, 2011

I usually code in PHP, C++ and ActionScript.I'm trying to follow an example of how to add images to a full screen application. What he does is that he adds a JPG background image, and then 4 PNG images. I tried to do it like I always do, by writing the code by myself looking at the book. It didn't work. I searched for errors in the code, changed some things, tried different things, but it didn't work. Then I tried to use his own code, that I downloaded from his website. That didn't work either.. I tried to find another way to add an image, and I can't seem to figure out a way to implement images in any other way into this class that's written in this book.. My Java programming level isn't just high enough.

Here's the code for the file where the images load, downloaded from the authors website (I've modified the brackets and some spaces so that it becomes easier to read):

Java Code:

import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class ImageTest extends JFrame {
public static void main(String[] args) {
DisplayMode displayMode;

[code]....

View Replies View Related

Constructor In Abstract Class?

Jun 23, 2014

Do we have constructor in abstract class? If we have then what is the use of it?

View Replies View Related

Loading File From Input

Mar 24, 2015

how I can change my code so that somebody can actually input the specified file path, rather than having it fixed in the code. I previously used

Scanner in = new Scanner(System.in);
System.out.println("What is the filename?");
String input = in.nextLine();
File file = new File(input);

But then the program would not display the frequencies.

import java.io.File;
import java.util.*;
/*
This program will allow the user to enter in a text file name, when prompted the program will anaylser the text and display the frequencies
*/
public class AssP {
public static void main(String[] args ) {
Scanner scan;
try {
Scanner scanner = scan = new Scanner(new File("C:/Users/Mary/workspace/Assingment/src/test.txt"));

[code]....

This is my current code and I need to so the user can load their own files.

View Replies View Related

Difference Between Abstract And Inheritance

Dec 16, 2014

I am new to java i dont understand the difference between the abstract and inheritance i mean we use the abstract class with extends with other class name ,even we do that same in the inheritance pls tell me main difference between abstract and inheritance...

View Replies View Related

Polymorphism Possible Only When Superclass Is Abstract?

Jan 23, 2014

I've a question regarding polymorphism.

Is polymorphism possible only when the superclass is abstract?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved