Creating Object Of Inner Class - Getting Error

Aug 23, 2014

package home;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Box{
int x=70;
int y=70;

[Code] ....

L a = new L(); causing the error. It will be great to know why it is showing error.

View Replies


ADVERTISEMENT

Creating / Using Object Class To Create Another Field In Another Class?

Jun 10, 2014

Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. There is also a MyDate class as explained below. A person has a name, address, phone number, and email address. A student has a status (freshman, sophomore, junior, or senior). Define the status as an integer which can have the value 0 (for "Freshman"),

1 (for "Sophomore"),
2 (for "Junior"), and
3 (for "Senior"),

but don't allow the status to be set to any other values. An employee has an office, salary, and dateHired. The dateHired is a MyDate field, which contains the fields: year, month, and day. The MyDate class does not explicitly inherit from any class, and it should have a no-arg constructor that sets the year, month, and day to the current year, month, and day. The MyDate class should also have a three-argument constructor that gets three int arguments for the year, month and day to set the year, month and day.

A faculty member has office hours and a rank. Define the rank as a String (for values like "Professor" or "Instructor"). A staff member has a title, which is also a String. Use data types for the fields as specified, or where one is not specified, use a data type that is appropriate for the particular field. Write a test program called TestEveryone.java that creates a Person, Student, Employee, Faculty, and Staff object, and invoke their toString() method (you don't need to call the objects' toString() method explicitly).

Note: Your MyDate.java class is the object class that your dateHired field is created from in the Employee.java class.

Do not use the Person, Employee or Faculty classes defined on pages 383 and 384 of the book. Create new ones.Here is the code I have so far concerning the employee and MyDate.

public class Employee extends Person {
private String office;
private double salary;
//private MyDate dateHired;
//7 argument constructor for employee
public Employee(String name, String phoneNumber, String email, String address, String office, double salary /*MyDate dateHired*/) {
super(name, phoneNumber, email, address);

[code]....

View Replies View Related

Creating Object Of Class And Calling Its Method In Different Class

May 20, 2015

In the process of creating a new class, I need to move my main method from the class SaveDate to the class DynamicTest. Below I have listed the code of both classes.The objective is to be able to run my program from the DynamicTest Class. I need understanding the process of moving my main method to a different class and creating an Object of a class and calling its method.
 
public class SaveData { 
  private static final Map<String, Object> myCachedTreeMap = new TreeMap<String, Object>();
   public static final List<String> getLines(final String resourceParam, final Charset charset) throws IOException{
  System.out.println("Please get: "+resourceParam);
  if (myCachedTreeMap.containsKey(resourceParam) ) {
  // Use the cached file, to prevent an additional read.

[Code] ......

View Replies View Related

Error - Creating Instance Of Inner Class

Mar 22, 2014

Java Code:

package Threads;
// THIS PROGRAM WILL HAVE TWO THREADS i.e. "main" AND ANOTHER THREAD (SYSTEM WILL NAME IT "Thread-0"
//THE STORY IS THAT WE WILL START Thread-0 FROM main AND LET IT EXECUTE.
//main WILL WAIT AND LET IT EXECUTE FOR 5 MINUTES.
//IF IT FINISHES ITS EXECUTION BEFORE 5 MINUTES, WELL AND GOOD;
//BUT IF IT DOESN'T, WE WILL INTERRUPT IT.
//AFTER INTERRUPTION, WE WILL DECIDE TO WAIT INDEFINITELY.

public class SimpleThreadsCopy {
public static void threadMessage(String s){
String sThreadName= Thread.currentThread().getName();
System.out.format("%s: %s%n", sThreadName, s);

[Code] ....

The statement against which I have written many *'s gives the following error.

No enclosing instance of type SimpleThreadsCopy is accessible. Must qualify the allocation with an enclosing instance of type SimpleThreadsCopy (e.g. x.new A() where x is an instance of SimpleThreadsCopy).

Now that a similar "error-free" code is given here, what's wrong with this piece of code and what should I do about it?

Trying to understand the error statement, I replaced the erroneous statement with

Java Code : Thread t= new Thread(new SimpleThreadsCopy().new MessageLoop()); mh_sh_highlight_all('java');

And the error got fixed. From that I understand that the inner class is just kinda a nonstatic member of the outer class and it will be accessed by the objects of the outer class only.

But then why doesn't the code in the tutorial give an error?

View Replies View Related

Passing Parameter From Object Of Class B To Object Of Class C By Use Of Class A?

Dec 13, 2014

Assuming that we have two classes B and C which inherit from class A. What is the best way to pass a parameter from an object of class B to an object of class C by the use of class A without using static variable and without defining a get function in B?

View Replies View Related

Creating New Constructor In Child Class Which Is Not In Parent Class

Feb 7, 2014

I've a parent class with a argument constructor like below(a sample code)

public class Parent {
Parent(String name) {
System.out.println(name);
}
public static void main(String[] args) {
}
}

Also I've child.class which extends Parent.class as shown below,

public class child extends Parent {
child(String name) {
super(name);
}
}

Now, I want create/modify the constructor which is in child, by taking "int i" as an input instead of "String name". How can I do that? Run time I want to execute child constructor not a parent constructor.

Condition is: Without making any changes to the Parent class

View Replies View Related

Creating Object Oriented TBG

Jun 9, 2014

I'm really new to object oriented coding. A good way to create a Text Based Game that used objects as rooms, and items. However, I can't figure out what the best way to define and use rooms and items would be. I've been at it for the past few days, and just can't think of something that works well and is easy to use.

I want to have each room have it's own "inventory" of items that the player can pickup, as well as drop items into. I want items that are dropped to maintain their properties (durability, level, ect). I would also need several methods for things like getting an item from a room by the item's display name or id. For instance, if there is a room with a Broken Iron Sword, and a Stone Dagger, I'd need to be able to get the Broken Iron Sword object with the string "Broken Iron Sword".

View Replies View Related

What Does Creating ArrayList Object In A Constructor Mean

Mar 22, 2014

import java.util.ArrayList;
public class LectureRoom{
private String courseName;
private String roomNumber;
private String Lecturer;
private ArrayList <Student> studentList;

[Code] .....

Question:

Given the following BlueJ class diagram

Lecturer class (same with previous lab, no changes needed)
Student class (same with previous lab, no changes needed)

LectureRoom (changes occurs here)

1. LectureRoom has roomNumber (e.g. A301), courseName (e.g. Java), lecturer (a reference to a Lecturer object), and studentList (a reference to an ArrayList that stores Student object).
2. LectureRoom has a constructor that receives courseName, roomNumber, and Lecturer. The constructor then sets/assign the courseName, roomNumber and Lecturer. This constructor also creates the studentList arraylist object.

View Replies View Related

How To Prevent Java From Creating Object

Mar 6, 2014

I just want to ask about a kind inheritance.Let say I have an interface MachineCode.I also have different classes, Binary, Hex and Octal that implements MachineCode

Question: How can I prevent java to create an Object like this:

Binary bin = new Binary();
Hex hex = new Hex();
Octal octal = new Octal();

those declaration above must be compile error,

I want to create Objects of Binary, Hex, and Octal this way:
MachineCode bin = new Binary();
MachineCode hex = new Hex();
MachineCode octal = new Octal();

View Replies View Related

Creating New Non-static Inner Class Outside Of Parent Class

Nov 22, 2014

I have an Abstract Class called GameColorEffect which contains a number of non-static Inner Classes that extend their Parent Class, GameColorEffect. I want to be able to create instances of the Inner Classes, however my IDE, eclipse, prompts me with the error:

No enclosing instance of type GameColorEffect is accessible. Must qualify the allocation with an enclosing instance of type GameColorEffect

And eclipse shows me a possible solution which is to turn the Inner Classes to static, this would allow me to create instances, but not really. This is because using methods from the static Inner Classes that change values in the Inner Classes will do this for every instance of the same Inner Class which is literally like a single instance. However, I want these Inner Classes to be individual with their values and still be able to use them outside as instances. I've found out a possible solution, which I'm not sure works like I want it to:

Java Code : GameColorEffect = new GameColorEffect.ExampleEffect(); mh_sh_highlight_all('java');

However, this is in-compact because sometimes all I need is to use just a method like:

Java Code : new GameColorEffect.ExampleEffect(intensity).applyEffect() mh_sh_highlight_all('java');

And another solution that I already knew prior was that I could make the Inner Classes proper classes not inside of the GameColorEffect class, but this is also in-compact because I will have to have so many classes for the so many effects that I have.

View Replies View Related

Super Constructor Call - Creating Object

Sep 18, 2014

Its written that every constructor calls its super class constructor. And we know, constructors are called to create an object. Does it mean that if I am creating an object of a class, I am actually creating objects of all its super class???

View Replies View Related

Declaring Object Using Interface Implemented By Its Class Vs Declaring Object Using Class

Apr 8, 2014

Suppose you have a generic Dog class of the pet type that implements a DogInterface of the pet type. What is the difference between;

DogInterface<pet> Rex = new Dog<pet>();

and

Dog<pet> Tye = new Dog<pet>();

In what situations might you want to use Rex instead of Tye?

View Replies View Related

What Is A Class Object Not Class Named Object

Mar 24, 2014

"You might wonder what happens when a static synchronized method is invoked, since a static method is associated with a class, not an object. In this case, the thread acquires the intrinsic lock for the Class object associated with the class. "What is a Class object associated with a class. Google search rather finds material about the Object class.

View Replies View Related

Linked Binary Search Tree - Creating Node Object

Apr 5, 2015

I'm trying to use LinkedBinarySearchTree but a lot of the variables are protected in BinaryTreeNode. I am creating a BinaryTreeNode object but it still isn't allowing me to use them. The variables I am trying to use are element, left, and right.

import ch11.exceptions.*;
import ch10.LinkedBinaryTree;
import ch10.BinaryTreeNode;
/**
* LinkedBinarySearchTree implements the BinarySearchTreeADT interface with links.

*/
public class LinkedBinarySearchTree<T> extends LinkedBinaryTree<T>
implements BinarySearchTreeADT<T>

[Code] ....

View Replies View Related

Creating TXT File - Program Keeps Printing Exception Error

Apr 10, 2014

I'm trying to have the program create a .txt file for me but it is not working properly. It just keeps printing the exception error "The file could not be found" and exits the program.

// This program asks the user for input for a name, phone number and notes up to 200 entries.
// It stores every contact in a file. Type 'h' for help while running this program.

import java.util.Scanner;
import java.io.*;
public class Phonebook {
static Entry[] entryList = new Entry[200];
static int numEntries;
public static void main(String[] args) {

[Code] ....

After running the code, I go into my workspace folder and I see that the file was created. What to do because running the program will always print "could not find the file".

View Replies View Related

Creating Menu Using Java - Animating Drawn Object On Button Click

Nov 7, 2014

i would like to create a menu like below where if i clicked on "DRAW", a frame to draw will appear and when i clicked on animate, I can animate an object drawn. I tried on using JButton, but I donno how to customize the design n so I would like to know is there other ways so tat my menu will appear as below?

View Replies View Related

Accessing Parent Class Method Using Child Class Object?

Feb 4, 2015

I want to know is there any way we can call parent class method using child class object without using super keyword in class B in the following program like we can do in c++ by using scoop resolution operator

class A{
public void hello(){
System.out.println("hello");
}
}
class B extends A{
public void hello(){
//super.hello();
System.out.println("hello1");

[code]....

View Replies View Related

Pass Private Final Class Object To Another Class Constructor

Aug 28, 2014

can we pass private final class object to another class constructor?

View Replies View Related

How To Create Object For Multiple Class Inside Single Class

Apr 22, 2015

How to create object for "class B" and call the "function_B" from other different class D where class D has no connection with class A? Here is my program.

public class A(){
void print(){}
}
class B{
void function_B(){}
}
class C{
void function_C(){}
}

Here, A, B, C are in the same package. But class D is in different package.

View Replies View Related

Interface Class And Object Class Is Compiling Symbol Errors

Nov 16, 2014

I am a beginner here at JAVA and I am trying to program a Gratuity Calculator using both interface class and object class but it keeps on compiling with errors saying "cannot find symbol".I tried everything to fix it but it just keeps on stating symbol.

[CODE]
public class GratuityCalculator extends JFrame
{
/* declarations */
 
// color objects
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
Color light_gray = new Color(192, 192, 192);
 
[code]....

View Replies View Related

Getting Error While Creating A Method - Void Is Invalid Type For Variable

Jun 25, 2014

I am new to Java and trying to learn it.I wrote the following program but while creating the method nav i am getting errors.

Error:- void is an invalid type for the variable nav

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.*;
public class YahooHomepage {
private static WebDriver driver;

[Code] .....

View Replies View Related

Should Instantiate New Object In Value Object Class?

Sep 22, 2014

I have a value object class and it has the below member
 
XMLDocument request = null;
 
Should I instantiate it in the VO class as
 
XMLDocument request = new XMLDocument();
 
or leave it null and let the calling program instantiate it?
 
What's the proper way to do it for Value Object classes in java ?

View Replies View Related

Methods From Original Class Receiving Error When In Test Class

Jul 5, 2014

I am working on a program that simulates a bug moving along a horizontal line, My code works correctly when I test it in it's own class but when I tried testing my constructor and methods in a test class I received an error saying, "package stinkBug does not exist" on lines with my methods. However, stinkbug is not a package.

Java Code:

/*
* 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.
*/

[code]....

View Replies View Related

Error Passing Value Of A Variable From One Class To Main Method Of Another Class

Jan 8, 2014

I've 3 classes.

1. Circle
2. GetInputFromUser
3. testCircle

package ABC;
public class Circle {
private double radius;
public double getRadius() {
return radius;

[Code] .....

In the testCircle class, in the line: getRadius = ui1.GetInput();

It's showing the error: The method GetInput(float) in the type GetInputFromUser is not applicable for the arguments ()

And when I do: getRadius = ui1.GetInput(rad);

It's showing the error: rad cannot be resolved

View Replies View Related

Creating Objects From Class

Aug 17, 2014

So I'm still trying to get to grips with Java, and like to understand exactly why I'm doing something, so that I am not just regurgitating the code, If I want to create an object from class "Apples", I would use the following, right?

Apples MyAppleObject = new Apples();

From what I understand, MyAppleObject is the new object name, new -> creates a new instance of it in memory, and Apples() is the onCreate method that is called

So question 1: (just a quick aside question) Can I create an object without calling Apples()? i.e.

Apples MyAppleObject = new;

Question 2: - PARTLY SOLVED - I discovered that (Button) is a way of typecasting, so I understand that line a little better. What I don't understand is why we don't need to initialize the object with "new"

I've now looked at a bit of android development and xml and those declarations are all together different, and I'm not sure why. I haven't found a single explanation for the difference in format.

Java Code:

Button Add;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Add = (Button) findViewById(R.id.button1); mh_sh_highlight_all('java'); So the Button object is declared above the onCreate method, but initialized afterwards I guess....

But instead of using Button Add = new Button() they use Add = (Button) findViewById(R.id.button1);

Question 3:

then In XML they use the following:

Java Code:

public*static*void*main(String[] args){
*********
********// Creates a DOM object in memory. Now you can access
********// data in the xml file
*********
********Document xmlDoc = getDocument("./src/tvshows5.xml"); mh_sh_highlight_all('java');

Once again, why didn't they have to use : Document xmlDoc = new Document()

View Replies View Related

Creating Class Inside A Interface

Jun 22, 2014

I am not able to understand how we are able to create an object of static inner class defined inside an interface .

interface outer {
class inner {
void disp() {
System.out.println("inside disp");

[Code] ....

output:

inside disp

How am i able to create a new object for static class?

View Replies View Related







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