Implementing Classes Instead Of Extending To Get Methods?

Sep 20, 2014

Basically I want to make a class called library, but I don't want to make an interface because I actually want to define the methods. I think I can only use abstract classes but not really sure how to use those. But I still have a problem, I want to create a Map that classes implementing Library class have to have in their code, and the Map will be a HashMap with <String, ParentClassHere>, so basically let's say I make a class called Car, implemeing Library to the Car class would create a Map library = new HashMap<String, Car>. Can I do something like this? And also include methods to get values and set values to the library Map?

View Replies


ADVERTISEMENT

Extending And Inheriting Classes

Jan 30, 2015

My teacher told me : It looks like everything is working except that in the Box, the method returns true if it is not a box.

The only other thing missing is the if statements in the main method using the .equals() method to do the comparisons. But, now I am lost on how to do my if statement.

HTML Code:
private int height;
/**
* Constructor for objects of class box
*/
public Box3(int l, int w, int h)
{
// call superclass
super(l, w);
// initialise instance variables
height = h;

[Code] ....

View Replies View Related

Extending Classes In Inheritance Hierarchy

Jan 4, 2015

I have a working program, except that it does not calculate the credit hours and the financial aid. When I enter an input, it works, until it should show the student name, credit hours and financial aid. the error i get from the command is "Hours invalid for false student". Here is the program i think i might have the problem.

import java.text.DecimalFormat;
public abstract class Student
{
//initialise variables
String name;
int creditHrs;

[Code] .....

View Replies View Related

Swing/AWT/SWT :: Extending Classes And Importing Libraries Basics?

Mar 12, 2015

1. Tried to use setLayout() but it wouldn't let me. Have imported libs

import javax.swing.*;
import java.awt.*;

but it doesn't work. It only worked when I extended a class with JFrame. Why do I have to do it? I already have a JFrame instance:

public test(){
JFrame frame = new JFrame();
frame.setTitle("Workshop");
frame.setBounds(500,300,400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
....
}

This code runs smooth, so it's confusing for me that it lets me create a JFrame object, and put buttons onto it, but it wants me to extend the class onto JFrame class in order to use a method.

2. I've read somewhere that AWT is old and should not be used anymore. Is there a diagram or a list with objects and properties that are manipulated only by Swing so that I could concentrate only on those? For example (made up list):

JPaper -

- width, setWidth(x)
- height, setHeight(x)
- position, setPosition(x,y)
- color, setBackground(RGBa)
- acquire, add(subordinate component)
- deprive, remove( subordinate comopnent)
- etc...

JSticker -

- width ...
- height ...
- write, setText(String)
- etc...

View Replies View Related

Implementing Methods - Draws Ovals Or Rectangles On Screen And Can Change Fill / Outline Color Of Shape

Oct 5, 2014

The following class is part of a homework assignment:

package event;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import model.Model;
import shapes.Rectangle;
import shapes.Shape;

[Code] .....

I have to implement the methods for what is happening if on some panel the action selected are Remove/Move/Change/Resize (Draw is already implemented).

The Applet in question draws ovals or rectangles on the screen and you can change the fill-color and outline-color of the shape. All this works already. I have dabbled with the move-method, but am not going anywhere.

View Replies View Related

Exchange Between Classes And Methods

Apr 14, 2014

I need to create a Dog class that represents a dog. Then, there are 2 different methods that in the end will each represent one dog each. Each printout will show the Name, Breed, Age, and Age in Human Years. Right now I'm having trouble trying to move methods in between each class. Here's the first class.

import java.util.Scanner; 
public class Dog { //no main method
static Scanner input = new Scanner(System.in);
//writeObject method
public static void writeObject(String name, String breed, int age) {
System.out.println("Enter the dog's name, breed, and age: ");

[Code] ....

View Replies View Related

How To Call Non Static Methods From Other Classes

Apr 16, 2015

how come you can call non static methods from other classes(objects when they are created from main) but not static methods in the same class as the main method??

example I cannot call the method maximum from the main method aslong as its not static BUT i can call other objects non static methods from main??

class test{
public static void main(String [] args){
Scanner input = new Scanner(System.in); //create new Scanner object
//for input
int number1;
int number2;

[Code]...

View Replies View Related

Returning Values Between Two Classes With Methods

Feb 4, 2015

I have a very standard Lab assignment. It's probably been seen a lot. I wrote the first part not realizing I had to write a second class to do use the methods. I'm not sure how to change my program to call methods from my second class instead of doing all my calculations with user input in my first class.

Here's the first class' code:

package tickets;
//Imports classes used for "Ticket" application.
import java.util.*;
import java.text.DecimalFormat;
public class Tickets {
 
[Code] ......

The code is obviously incomplete. I have not tried to compile, nor would I expect it to compile right. I'm not sure how to move my calculations from the first class shown above into my second class and use them as methods.

View Replies View Related

How To Access Methods From Other Classes Using Buttons In Javafx

Jun 4, 2014

I have a javafx class that has buttons and textfields. Here is the code for a button, and i want the button to make a new object but im having trouble setting the constructor

Label label = new Label("Type");
GridPane.setHalignment(label, HPos.RIGHT);
TextField textField = new TextField();

Label label2 = new Label("First Name");
GridPane.setHalignment(label2, HPos.RIGHT);
TextField textField2 = new TextField();

[Code] ....

after i create the object i will insert the object in an arraylist of person objects

View Replies View Related

Java Classes - Trace Methods Calls

Apr 1, 2015

Consider the two simple Java classes below:

Java Code:

class Computer {
Computer() {
System.out.println("Constructor of Computer class.");
}
void On() {
System.out.println("PC turning on...");
}
void working() {

[Code] ....

After the program run, how do I trace (1) which object call which method (2) and how many times?

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

JSF :: Extending Primefaces 5 Components

Jul 25, 2014

Really need some simple and complete example on how to extends components (graphically and functionally) for primefaces 5.

For example I can't figure how to add custom attributes to existing component or define default values for existing attributes.

Is there any tutorial or a basic common way to achieve this goal or each component have to be extended in its way?

View Replies View Related

Constructor Error When Extending A Class?

Oct 21, 2014

I am writing a program that should take a url and scan the page for any links. It is in the beginning stages, but I ran into an error when I tried to extend a class. There's a lot going on in this code, but the error is caused by the constructor.

Error message at compile time:

"constructor Page in class Page cannot be applied to given types;
{//Constructor
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length".

Here's the code(first my class, then the class I extended)

public class SearchEngine extends Page {
public static Color customGreen = new Color(69, 194, 33);
public static Color customYellow = new Color(232, 166, 12);
public static Color customBlue = new Color(25,97,255);
public static Color customYellowComp = new Color(178,125,0);

[code]...

View Replies View Related

Java 2D Game - Use Canvas Without Extending It

Apr 10, 2014

Java Code:

public class Game extends Canvas implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;

public Game() {
setMinimumSize(new Dimension(width, height));
setMaximumSize(new Dimension(width, height));
setPreferredSize(new Dimension(width, height));
}

[code]...

I want to remove the extends Canvas from the top line and when I use JFrame to add canvas, I don't do add.(this, but instead I do add a canvas variable I make. So instead of extending I want to make a variable. But then how would I do the start, stop and run?I want to use Graphics, Canvas (Not Jpanel because I want to use BufferedImage) & JFrame. I don't want to extend Canvas, how could I use Canvas identically to shown above but instead of extending using it as a variable? or, how could I do this?

View Replies View Related

JavaFX 2.0 :: Extending SplitPane Correctly

May 26, 2014

I'm having a problem with SplitPane (horizontal Flow). What I'm looking for is to find a way to maintain  the left pane in the same position when the main window maximizes. I do not want the left pane to grow on its width. If I go into the AnchorPain Constraint in the Scene Builder and remove the right and left constraints, when I run the application, the left pane keeps its position, but it appears a gap between the two panes (the left pane and the right one).
 
I need to find the way to expand the right pane to the position where the left pane is.
 
The link shows an image that illustrates the problem. [URL]

View Replies View Related

Difference Between Extending JFrame And Just Creating One In The Class?

Oct 18, 2014

What is the difference between extending JFrame in one class and simply constructing a new JFrame object in that same class? What benefits do I have with each solution, providing I want to use that class to create the GUI. Is it the same or are there differences rather than not having to reference to a new JFrame to be able to use its functions?

View Replies View Related

Extending Calculator - Added Function To Square Numbers

Feb 14, 2014

I am new to java and during my classes we built a basic calculator. I want to extend my calculator and added function to square numbers.Here is my program:

Java Code:

package calculator02;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

[code]...

View Replies View Related

Unable To Pull Data From Another Class Into GUI Already Extending JFrame

Apr 26, 2014

I've been trying to pull data from another class file "Calculations.java" and have it be displayed in a TextField in "DataAnalyzerGUI.java". Here is how the hierarchy is broken down for my assignment:

DataAnalyzerGUI.java extends JFrame
ReadFiles.java extends DataAnalyzerGUI.java
Calculations.java extends ReadFiles.java

Everything displays and functions correctly in a command prompt if I use a line like this:

System.out.println ("Lowest opening " + dateArray[lowestOpenIndex] + ": " + dataArray[lowestOpenIndex][2]);

But trying to get it to display in a GUI has been quite troubling.

I know the code is supposed to look something like this:

dataOutput.setText(DESIRED CODE HERE);

But I am unable to find anything of value to work out.

I have attached my complete project....

Attached File(s)

Assignment 4.pdf (247.05K)
Assignment 4.zip (235.81K)

View Replies View Related

Extending Static Inner Class Defined Within Inheriting Class?

Jul 3, 2014

I am working on a project involving a class that has the attributes of one of its inner classes. Now, if possible, I would like to make it so that the inner class is not visible outside of the class. Also, some of the functional mechanics require that the class be an instance of the nested inner class (it extends the inner class). The following code snippet demonstrates the situation.

public class A extends A.B {
public static class B { //ideally I would like this to be private/protected.
}
}

When I try to compile this program, I get the error message "Cyclical inheritance involving A." This error does not make much sense because, since the inner class "B" is static, it requires no instance of "A" (it does not inherit from "A" or uses it). My question is "Is it possible to do something similar to this structure?" I have searched many forums in search of the answer but have not found anything that attempts to explain it. The closest problem that I have found is one relating to the inheritance of a nested inner class from another class. I would like to express that the problem that I am having involves a class defined within the inheriting class.

View Replies View Related

How To Call Classes Within Other Classes

Apr 18, 2014

How do you call classes within other classes? Or can you only call classes through the main?

View Replies View Related

Implementing A FPS Counter

Jan 17, 2014

Ok, so I'm making a game with a space ship that flies around the universe and discovering new planets. It works fine so far, but I'm looking to make it perform better and be better compatible with lesser-processors.So, I'm trying to put in an FPS counter and an entirely new game loop so that my game can decide for me what FPS I should use.

I have two classes. Heres the big, main one: [Java] package cyentw.game.src; import java.awt.Color; import java.awt.Font; impor - Pastebin.com *I want to change the loop and put an FPS counter in around line 456, you can scroll past the rest if you'd like*And heres the init frame one, in case you'd like to see it for some reason.

Java Code:

package cyentw.game.src;
import javax.swing.JFrame;
public class Start extends JFrame{
public static JFrame frame;
public static int WIDTH = 500;
public static int HEIGHT = 500;
public Start() {

[code]....

how to make my game loop as quickly (or a bit slower) as it can, and my FPS is static.

View Replies View Related

Swing/AWT/SWT :: Implementing JTabbedPane GUI?

Aug 21, 2014

I have been trying to learn how to use the TabbedPane GUI. I can get the tabs to show up, but the buttons I have placed in each tab do not show up. Why this is not working. I assume that, for some reason, the buttons are not linking with their respective panels, or the panels are not linking to the respective tabs.

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

[Code] ....

View Replies View Related

Implementing Comparator Interface?

Mar 7, 2014

overriding of the compare method.

Here's an example I found:

public class Someone {
String name;
int age;
ArrayList <Someone> listarr = new ArrayList <Someone>();
public Someone(String name1, int age1) {
name = name1;
age = age1;

[code]...

1. In the compare method, what happens when it returns one of the 0, -1, or 1? How does returning such values contribute to the sorting process? How does it take in information (like I know it does through the parameters, but how exactly)?

2. Why do we use the class name for the generics?

View Replies View Related

Implementing Two Stack In One Array?

Jan 5, 2015

is this program correct? for Implementing two Stack in one array. how can i solve it ?

public class Stackation11 {
int max = 10;
int [] array = new int[max];
int top;
int top1;
Stackation11(){

[code]....

View Replies View Related

Implementing Custom Map Interface

Nov 5, 2014

I am supposed to implement a custom Map interface and I'm having some trouble with this method:

// 1. From the interface
/**
* Gives an iterator iterating over the key set from the smallest key that is not less than the key provided.
* @param key the key
* @return the iterator
* @throws NullPointerException if key == null
*/

public Iterator<Key> tailIterator(Key key);

[Code] .....

My implementation is wrong according to a JUnit test. Also, to get a full score this method should have a worst case running time of O(log N), which obviously isn't the case now. (My Map implementation is currently based on binary search in an ordered array, keeping a prallel array for the values).

View Replies View Related

Implementing Breadth First Search

Dec 13, 2014

I have always wanted to program A*, Breath First Search and Depth First Search ever since I took AI. I will only post BFS at the moment but it is only searching the row.

Java Code:

//Breath First Search. The first node passed to it is the start button
void bfs(Mybuttons button){
Queue<Mybuttons> q = new LinkedList<Mybuttons>();
q.add(button);
button.setIcon(button.setButtonToBlueWhileSearched());
button.model.visted=true;

[Code] ....

Output:

Java Code: In while loop
In get Child Node
In get Child Node
In get Child Node
In get Child Node
In while loop
In get Child Node
In get Child Node
In get Child Node
In while loop
In get Child Node
In get Child Node
In while loop
In get Child Node mh_sh_highlight_all('java');

View Replies View Related







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