Referring To Object That Has Not Yet Been Made?

Apr 16, 2015

I am trying to make a for loop using an iterator that refers to an object of a class that hasnt been made yet. The class is called MyArrayList but it does not have a Main. A MyArrayList object is made externally so I am unsure how to refer it to the object from within the class. Is there a place holder I could use such as Object.Iterator() or something similar. I have created an inner class that implements the Iterator interface. Here is the code that I need to adjust to do this:

public class ListIterator<E> implements Iterator<E> {
public myIterator() {
Iterator it = MyArrayList.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}

View Replies


ADVERTISEMENT

Servlets :: Errors Referring To Old Database

Apr 21, 2014

I implemented the new DB, I have changed all the variables accordingly. When I execute the website, I get errors and it keeps referring to the old db.

Using sql langauge, the website has been implemented in Java.

View Replies View Related

Referring To Overridden Method From Supertype Reference?

May 14, 2015

If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you're calling the supertype version of the method.

is this true? maybe i'm misunderstanding it, but i thought the JVM looks at the object at run time and checks the object type. the context of the quote is about checked exceptions, but it seems like the statement should stand regardless of context. but this doesn't back up my experience. for example:

public class Test{
public void print(){
System.out.println("Super");
} public static void main(String[] args){
Test t = new SubTest();

[Code] ....

Will invoke the subclass method. like i said, maybe i'm missing something.

View Replies View Related

How Recursion Calls Are Made

Jun 5, 2014

How the recursion works. I tried to figure out writing down low, mid, high at each recursive call. But I seem to be making a mistake somehow. I don't understand where the values are returned to in

if(leftmax>rightmax){
return leftmax;}
else
return rightmax;

Here's the code:

public class Maximum{
public static int max(int[] a,int low,int high){
int mid,leftmax,rightmax;
if(low==high)
return a[low];

[Code] ....

firstly leftmax=max(a,0,4)

Then what is the next line executed?Is it rightmax=max(a,5,8).

After this is it leftmax=max(a,5,6)

rightmax=max(a,7,8)

I tried to understand what these recursion calls by writing them down.But I somehow make a mistake.

View Replies View Related

Two Windows Are Being Made Of Game

Jan 19, 2014

I have a game which is very simple: it is two images, and one needs to reach the other. Once it does, it resets. I have a class which holds the two images, and another which holds the game. My images class extends my game class. The problem I am having is that when I run the program, not one, but two frames appear. One is the perfect game, working perfectly, and the other is the game without images. I want the one that is the game without images to simply stop appearing. I believe it is appearing because my class that displays the image is extending the game class, but I can't not extend it because if I do I get a whole lot of bugs and errors.

Image class:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

[code]....

View Replies View Related

Displacing A Shape Made Of Asterisks

May 5, 2014

I'm making a program that prints arrows of varying tail length, head width, and displacement.The shape algorithms are working properly, but the displacement isn't working. The first arrow has zero displacement, and the second one is supposed to be bigger and start the print at 10 spaces right and 5 spaces down from the default, but it's not working. I tried changing the displacement settings of the first arrow, and no changes happened, so I know that the positions aren't registering properly in general. How to pass the xAdj and yAdj (displacement) values? I think I have the empty space formula set up, but it's still not running.

Driver:
public class Driver
{
//default values
public static final int Arrow_Default_xAdj = 0;
public static final int Arrow_Default_yAdj = 0;
public static final int Arrow_Default_tail = 5;
public static final int Arrow_Default_width = 5;

[code]...

View Replies View Related

Increase Counter When A Post Is Made?

May 15, 2015

I was wondering what the easiest way would be to keep track of everytime someone makes a new post on reddit (social media site, for those who don't know). I want the program to keep track of the number of posts made with a counter, but that's the easy part. How do I set up a program to actually communicate with and monitor reddit's servers in this manner?

View Replies View Related

JComboBox - Different Choices Depending On Selection Made

Oct 5, 2010

I am stuck trying to build a relatively "simple" GUI. The GUI should have one JComboBox that allows the user to pick from one of three locations. Then based on their choice, another JComboBox should be generated with another set of choices. The key is that the second ComboBox should have different choices depending on the selection made in the first one. So, in theory, the logic should be something like:

//create new JComboBox array of strings "locations"
//create new JComboBox array of strings "costToTravelThere"
//if user picks location 1 from the first ComboBox
then add the second ComboBox with options specific to that first location
else if user picks location 2 from first ComboBox
then add the second ComboBox with options specific to that second locatoin

I understand how to do the basic stuff, like create the JFrame and the actual panels and ComboBoxes, etc. What I don't understand is how to implement the second ComboBox dependent on the first one. I'm not very experience with Listeners and such, and I'm sure that's a big part of this.

View Replies View Related

Sort 2 Different Objects In Self Made Generic Class Using Comparator

May 31, 2014

Ok here are my 2 Classes

Both have Identical Fields

package com.Lists;
public class EmployeeOffice implements EmpInterface {
private double salary;
private String name;
private String postion;
private double hoursWorked;

[Code] .....

So if i wanna sort this Generic class using comaparator what do i do... I cant find an answer to this... I wanna sort them on the basis of salary what to do ...

View Replies View Related

Add Color To Ball In Pong - Speed Up When Contact Made With Paddle

Apr 23, 2014

I wrote a code from our text book for Pong. Im trying to figure out how to fill the ball with color. Im also wondering how to speed the ball up once there is contact with the paddle. The initial speed is ok but once contact is made with the paddle, it slows down. Makes it too easy to play. Here is my code so far. As far as the game goes, it works and is good. Just wanted to add a couple customizations.

Java Code:

package pong;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

[Code] .....

View Replies View Related

Change Type / Custom Color Of Rectangle Made Through DragMode

Jun 9, 2014

What I'm trying to do is basically draw a Rectangle outline through left-click and then dragging the mouse to the size one wants.

After one has created this rectangle, the user can change the color of the rectangle through clicking the 3 sliders found on the top in the program. Upon the change in color, the rectangle should change from "g.drawRect" to "g.fillRect" and use the desired color.

I have the basic outline of the program done, but working with the last part (the color and changing of the Rectangle type after changing the color)

Is there like a "then" command in Java? Like: after you've done x, do y. That would make this heaps easier.

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
 
//Diese Applikation offnet ein Fenster,
//in welchem durch einen gehaltenen linken Mouse Klick
//ein Reckteck gezeichnet wird
 
public class WhatIsTheProblem006 extends Applet
implements AdjustmentListener {

[Code] ....

View Replies View Related

Equals Method - Take Object Reference And Return True If Given Object Equals This Object

Mar 28, 2014

Create an equals method that takes an object reference and returns true if the given object equals this object.

Hint: You'll need 'instanceof' and cast to a (Geocache)

So far I have:

public boolean equals(Object O){
if(O instanceof Geocache){
Geocache j=(Geocache) O;
if (this.equals(j)) //I know this is wrong... but I can't figure it out
return true;
}

else return false;
}

I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?

View Replies View Related

Get Object Strings To Print In List So That User Can Select That Object To Manipulate Its Attributes

Oct 7, 2014

I am new to Java and have read books, the Java docs, and searched the Internet for my problem to no avail. I have an Array of objects that contains strings. How can I get the object's strings to print in a list so that the user can select that object to manipulate its attributes? For example, the user can select "Guitar 1" from a list and manipulate its attributes like tuning it, playing it, etc. I have a class called Instruments and created 10 guitar objects.Here is the code:

Instrument [] guitar = new Instrument[10];
for (int i = 0; i < 10; i++) {
guitar[0] = new Instrument("Guitar 1");
guitar[1] = new Instrument("Guitar 2");
guitar[2] = new Instrument("Guitar 3");
guitar[3] = new Instrument("Guitar 4");
guitar[4] = new Instrument("Guitar 5");
guitar[5] = new Instrument("Guitar 6");

[code]...

View Replies View Related

Swing/AWT/SWT :: How To Move A Object And Make Object Shot Laser

Mar 27, 2015

can a keyevent in java make the space ship and the laser too ?

View Replies View Related

Inheritance Relationship Between Type Of Actual Object And Object Reference?

Apr 15, 2014

For example I create an object like this:

BankAccount b = new SavingsAccount();

Now lets say that I want to access a method 'addInterest()' that is in the 'SavingsAccount' class I would have to do: '((SavingsAccount)s).addInterest();'

The question I have is why do I have to cast 'b' to SavingsAccount? Isn't the actual object reference of 'b' already an instance of 'SavingsAccount' class? How does the 'BankAccount' affect the object itself? I'm really confused as to what class is truly getting instantiated and how BankAccount and SavingsAccount are both functioning to make the object 'b'.

View Replies View Related

Object Reference Variable Unwilling To Be Reset To A Different Object Type

Nov 6, 2014

I don't understand why the object reference variable 'a' cannot be recast from a thisA object reference to a thisB object reference.Is it the case that once a reference variable is linked to a particular object type then it cannot switch object types later on.I am facing the Java Associate Developer exam soon and I am just clearing up some issues in my head around object reference variable assignment,

class thisA {}
class thisB extends thisA { String testString = "test";}
public class CastQuestion2 {
public static void main(String[] args) {
thisA a = new thisA();
thisB b = new thisB();

[code]....

View Replies View Related

Type In A Name And It Will Search Through Each Object And Print Back Corresponding Object Info

Nov 19, 2014

I am trying to get this to where I can type in a name and it will search through each object and print back the corresponding object info.

Java Code:

import java.util.Scanner;
public class MyPeople {
public static void main(String[] args) {
Person[] p = new Person[] {
new Person("Chris", 26, "Male", "NJ", "Single"),
new Person("JoAnna", 23, "Female", "NJ", "Single"),
new Person("Dana", 24, "Female", "NJ", "Single"),
new Person("Dan", 25, "Male", "NJ", "Single"),
new Person("Mike", 31, "Male", "NJ", "Married") };

[code]....

View Replies View Related

Create Object Deriving From PrintingClass And Use That Object To Print Text

Apr 9, 2014

Task:The main method of the class Things below creates an object called printer deriving from the class PrintingClass and uses that object to print text. Your task is to write the PrintingClass class.

Program to complete:
import java.util.Scanner;
public class Things {
public static void main(String args[]) {
String characterString;
Scanner reader = new Scanner(System.in);
PrintingClass printer = new PrintingClass();
System.out.print("Type in the character string for printing: ");
characterString = reader.nextLine();
printer.Print(characterString);
}
}

// Write the missing class here

Note: In this exercise the solution is part of a conversion unit where many classes have been declared. Because of this the classes are not declared as public using the public attribute.

Example output

Type in the character string for printing: John Doe

John Doe

My Class:
class PrintingClass {
public void print(){
System.out.println(characterString);
}
}

View Replies View Related

Display Properties Of Each Object Instead Of Object Array List Itself?

Mar 23, 2015

If I set a Jlist to contain an array of objects, how can I display properties of each object instead of the object array list itself. Ex:

Instead of:

Person1
Person2
Person3

display values such as each person name within the Jlist:

Mike
Paul
Andrew

View Replies View Related

Searching For Object In Linked List Then Removing The Object

Nov 19, 2014

I have just started working with linked lists. I have a linked list of Objects and I want to be able to search for a specific object. But currently my code continues to return false. Also how would I go about removing the first index of the linked list.

public static void main(String[] args) {
LinkedList<Cookies> ml = new LinkedList<>();
int choice = 0;
while (choice >= 0) {
choice = menu();

[Code] ....

View Replies View Related

Practical Use Of Multiple Object References Pointing To Same Object

Dec 27, 2014

I am reading Head First: Java and got to Object References. In the book I got a little bit confused on what happens when two object reference's point at the same object so I wrote a small crude test, the below code. This of course clarified what happens but what I am interested in knowing is in what circumstances would you want to have two separate references for the same object when you could just use the original? Eg. v1

class ObjectValue{
int objVal = 1;
}
class ObjectValueTestDrive{
public static void main(String [] args){
// "Value of v# should be" refers to if it copied the given object values, instead of referencing the same object
ObjectValue v1 = new ObjectValue();
System.out.println("Value of v1 should be 1:" + " "+ v1.objVal);

[code]....

View Replies View Related

Anonymous Object - Can't Understand One Time Usage Of Object

Aug 18, 2014

Explain anonymous objects with example clearly...i read some where anonymous objects advantage is saving memory...it is benificiable when there is only one time object usage in our program..i can't understand one time usage of object ....i know anonymous objects but i don't know in which context we use them in our programs...i did the anonymous object program with my own example but i can't differentiate this one with normal object..i'm providing my own example below

//anonymous object
public class anonymous {
int x=10;
int y=25;
void display()
{
System.out.println("anomymous");

[code]....

View Replies View Related

Swing/AWT/SWT :: Have Object Notify Main Object

Jan 15, 2015

What I want to do is have a label that is updated whenever an object gets some new, relevant data.The way you do it in Java looks different from the way we do it in Objective-C. In Objective-C, we have what's known as a protocol. An Objective-C protocol is almost exactly like a Java "implementation." In Obj-C, if I want the user to see the address of where he is, I can have an object that gets the information and invokes a view controller's method; at that point, the view controller would then take the data passed to it and display the data in a label. However, the view controller is an instance of a subclass of the bundled view controller class.

View Replies View Related

Swing/AWT/SWT :: Pass Object With Properties To Another Object

Jun 11, 2014

I am trying to find either some references to point me on the right track with passing an object with all of it's properties still in tact after it's been created. Currently I am trying to do this through an interface but it seems to just create a new object everytime without the properties. Example below :

import java.util.ArrayList;
import java.util.List;
public interface TPerson{
//public Person p = null;
}
class Thrower {
Person p;

[code]....

When I implement the interface on the other objects as soon as I call the setP method shown above it seems to just create a new one even though I pass the object to the method I want to use.

View Replies View Related

Difference Between Object Reference And Object?

Dec 1, 2014

difference between object reference and object

View Replies View Related

Calling Object Method From Another Object

Oct 26, 2014

I've been trying to learn Java for the last 36 hours or so (after applying for a HTML/CSS job saying "Java knowledge preferred"), and decided to experiment a bit making a graphical tic-tac-toe game. I eventually managed to get that done and it's working. Working code below:

[Java] tic tac toe 1 - Pastebin

So, it works to an extent, however, the way I am capturing which cell is selected seems very sloppy, and would not work if the cells weren't squares or rectangles. So I made a copy of the project and restructured it adding the mouse event to the cells, but now I can't get JComponent to repaint. New code below:

tic tac toe 2 - Pastebin

Curiously, clicking triggers the action for all 9 cells, but I presume it's because I haven't bounded them making it think I've clicked all 9 simultaneously.

What I've tried:

Make the Cell class extend the game class and call this.repaint()- causes stack overflow.

Calling Game.GameState() within the cell clicking event and making that function static - compiler doesn't like calling repaint() inside a static function.

Making another class to make a clone of the Game object and then refresh- was never going to work....

View Replies View Related







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