Adding Class Objects To HashSet?

Nov 18, 2014

Having trouble adding Class (Dollar) objects to a HashSet (money), i have done this before with arraylists and i understand that HashSets are different in that they cannot contain duplicates. Currently when this code is compiled i am getting "null" printed when I run the "howFullDatWallet" method.

import java.util.*;
public class Wallet {
private HashSet<Dollar> money;
private int walletSize = 0;
private int walletFiller = 0;
/**
* Constructor for objects of class Pocket
*/
public Pocket(int walletCap)

[code]....

View Replies


ADVERTISEMENT

Adding Objects To ArrayList

Oct 17, 2014

I've two classes: Student and ArrayListExamples (which has my main method in it).

The student class acts pretty simply at the moment with a constructor that takes a name, surname and ID number. I've tested it and it seems to be working. My issue is with adding an object to an array list, here is my effort:

public class ArrayListExamples {
private static int MAX_SIZE = 50;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList();
 
[code]....

I know the last loop to add the student objects is wrong.I'm calling the student ArrayList add method with a student object whose constructor requires two strings and an integer. Why isn't this allowed?

View Replies View Related

Declaring Methods For A Class In Its Own Class Whilst Objects Of Class Declared Elsewhere?

Mar 5, 2015

How do you declare methods for a class within the class whilst objects of the class are declared else where?

Say for instance, I have a main class Wall, and another class called Clock, and because they are both GUI based, I want to put a Clock on the Wall, so I have declared an instance object of Clock in the Wall class (Wall extends JFrame, and Clock extends JPanel).

I now want to have methods such as setClock, resetClock in the Clock class, but im having trouble in being able to refer to the Clock object thats been declared in the Wall class.

Is this possible? Or am I trying to do something thats not possible? Or maybe I've missed something really obvious?

View Replies View Related

Adding New Object To The End Of Array Of Objects

Feb 8, 2015

I am trying to build a method that takes an array of object and adds a new object of that type to the end of it . ONLY ALLOWED TO USE ARRAY , NO ARRAYLISTS VECTORS ECT . i realize that if the array is full you can use the java copy array method to make a new array and double its size until a certain point . The MAX AMOUNT OF OBJECTS is a constant that is 100 but for some reason even when executing my code i keep getting null pointer exceptions or index out of bounds errors , i have written this method many times now with out any success.

My question is how do I write a method that adds an object to the end of an array and if there are no spots left copies the current array into a new array and extend the size

private Animal [] objects;
final int MAX_ANIMALS = 100;
public AnimalObject()
{
objects = new AnimalObject[MAX_ANIMALS];
}
public AnimalObject(Animal[]a)

[Code] ....

View Replies View Related

Adding Points Objects Into Array

Oct 24, 2014

I am trying to add Point objects into an Array.This is my code to read in data

Java Code:

public void readRoadMap(File road) {
try{
String line;
BufferedReader br = new BufferedReader(new FileReader(road));
while((line = br.readLine()) !=null){
this.points.add(new Point(line));

[code]....

View Replies View Related

Adding A Phone Object Into Student / Employee Objects

Jun 1, 2014

I have to read in a file that that has Student, Employee, Faculty....such as name, last name, salary...etc

Example of a file input
Java Code: Student, John, Doe, [email]johnDoe[At]yahoo.com[/email], 123,Wonderland Ave, 21323,TX
PhoneNumber,Cell,111,222,3333
Student, Jesus,Satan,[email]jesus[At]satan.com[/email,666, HeavenHell Dr., 666666,CA
PhoneNumber,Work,111,333,5555 mh_sh_highlight_all('java'); Java Code: while(input.hasNext()){

[code]...

There is more code, but it's repetitive. Now, I keep getting an error. Array of bounds. I believe i get the error because the phone number. The phone number is on the next line..I created a different arraylist for phonenumber, but I dont know how to match it with the correct person, student, employee...etc.

View Replies View Related

Swing/AWT/SWT :: BorderLayout - Adding Multiple Video Game Oriented Objects Such As Sprites To JFrame?

May 24, 2014

I decided to write a small Java program to experiment around with BorderLayout, because I'm developing a Java game and I need to have 2 objects placed in a single JFrame at the same time, and everyone I asked said I need BorderLayout to do that.Before you answer: Also, is using BorderLayout the best option for adding multiple video game oriented objects such a sprites to a JFrame?

So the Java program I wrote is supposed to place a JButton on the JFrame and ALSO place a graphic component (a rectangle in this case). The problem is, only the button shows up, as can be seen in the image link below: URL....

Here is the code:

**main.java** --> The main method class + JFrame/JPanel/JButton constructor
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class main {

[code]....

View Replies View Related

Program With Hashset

Dec 10, 2014

A file has the data of the subjects and name of professors. The file contains line for each subject. The line starts with professor Name followed by the Subject name.

Harry Williams Advanced DBMS

James H Computer Networks

Sasha Ben Artificial Intelligence

Harry Williams Software Engineering

Palbo Kastro Formal Languages

Alex W Advanced SE

James H Operating System

Harry Williams Theoretical Foundation

Write a program with setter getter method to read the file, and then write a class to store several professors in a hashset (Single Key and Multiple Values).The program should be able to display all the professors in the set and allow the user to search for a professor.

Input:

Professor Name: James H.
Then the Output will be:

Subjects Taken by the professor: Computer Networks, Operating System.Display No Classes available if the professor name does not exists .

View Replies View Related

Write Hashset To Txt Using Filewriter

Mar 23, 2015

I am trying to write a hashset to a text file. Normally, I have no issues with writing to txt as it is simple for me. However, in this case I am absolutely clueless.

A bit of background about the class: it is a hashset of ComputerScientist objects with 2 fields; name, and field of research (and yes, I know what I put to fill up the HashSet does not count, I was only trying to test to see if I could get this to work).

I know the basic setup to use filewriter to save strings to a txt, but I am getting confused with the HashSet element of it.

import java.util.HashSet;
import java.io.FileWriter;
import java.io.IOException;
/**
* Write a description of class ComputerScientistSet here.
*/
public class ComputerScientistSet {
private HashSet<ComputerScientist> computerScientistSet;

[Code] .....

View Replies View Related

String Comparisons / HashSet And Duplicates

Apr 20, 2014

I have a HashSet, which I created to prevent duplicates upon output, but of course it's printing duplicates(or else I wouldn't be posting this). The order of my output does not matter, nor the input. The data type is String in the format (x + "," + z), where x and z are integers, creating a collection of coordinate sets. So to prevent the output of duplicates, I'm trying to get rid of the duplicates before they are added to the collection.

I've tried doing a '.equals()' string comparison but what happens is, since my string is added via one variable, it compares itself to itself and if itself equals itself it won't be added to the collection. I really need to keep this as a comparison of a single variable, because creating a key for each value would be sooo ridiculous for this volume of inputs.

So, with that being said, I would like to add one copy of the string, discard the duplicates, and do this thousands of times..

View Replies View Related

What Is The Difference Between HashSet TreeSet And LinkedHashSet

Mar 12, 2014

What is the difference between HashSet, TreeSet and LinkedHashSet?.One difference between HashSet and TreeSet is that TreeSet is sorted, whereas HashSet is not sorted. I dont know the other differences

View Replies View Related

Hashset - Java Date Validator

Apr 3, 2015

I'm fairly new to java, and is just building small programs to get the feel for the language. How I can improve this code.

import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

enum DateValidator {
DDMMYYYY(0, 2, 4),
MMDDYYYY(2, 0, 4),
YYYYMMDD(6, 4, 0);

[Code] ....

View Replies View Related

Adding A JButton From One Class To Another

Apr 25, 2014

I'm just learning JFrame and the like, I have 3 classes. I have one class that just calls the methods from the other 2 classes (my superclass), I have one class that draws a frame and adds a panel and then I have my 3rd class, where I'm trying to add a button from in the class to my panel in the other class. However it won't add the button. I'm sure I'm making some obvious mistake, as I'm quite new to Java, but I've been messing around with it for ages and I can't fix it. I'm also not sure if Frame should be extending Test or not.

import javax.swing.*;
import java.awt.*;
public class Test
{
JPanel panel;
JButton button1;

[code]...

View Replies View Related

How To Refer To Objects In Die Class

Apr 4, 2015

how to refer to objects die1 and die2 in the PairofDie class from the SnakeEyes class and what to do.

public class Die {
 
private final int MAX = 6;
private int faceValue;
private int sum;
//private int faceValue2;
 
public Die(){
faceValue = 1;

[code]....

View Replies View Related

Getting Objects Data In Class

Mar 7, 2015

I have a class which connects with database and fills object with data. Method looks like this:

//in database class
void getData(anObject a, int id){
//connects with database
a.field1=get...
a.field2=get...
....
//disconnects
}

Now I want to use this method in anObject class, but I can't call it because I don't have created object jet. Is there any way to call this method?

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

Adding Text To JTextArea From Second Class?

Feb 4, 2015

Basically , I'm trying to make a program that makes the Finch follow the object. I have made two classes :

NewOption52 and FollowClass.

Class NewOption52 contains a method which determines the properties of the GUI. Class FollowClass contains a main method which calls GUI method from class NewOption52 and it also contains several methods which dictates the Finch's behavior alongside with appending text to JTextArea feed.

When I connect the Finch and run the program , a GUI should appear and inside JTextArea should have text which says ""Please Place An Object in front of Finch And Then Tap Finch to Activate!". It didn't happen when I run the program.

Class NewOption52 :

import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.*;
public class NewOption52

[code]....

View Replies View Related

How To Find How Many Number Of Elements Are Landing In Same Bucket In HashSet

Jan 12, 2015

Is there any way to find how many number of elements are landing in same bucket in HashSet. I know Reflection could be one way but i am not able to design a program for that.

View Replies View Related

HashSet And Iterator - Accessing Element Contained In The Object?

Sep 23, 2014

I'm not new to java but i'm not able to solve the following issue: I have  a class

public class Localizzazioni implements java.io.Serializable {
private <complexType>  id;
public getId().......
public setId().....

The complexType is a class defined in the code somewhere. Now I want to access it in another class I have
 
Set localizzazioni = new HashSet(0);
localizzazioni=opere.getOiLocalizzazioneOperas();    -- this object give an object of tyoe HashSet
for(Object object : localizzazioni) {
  object.get.........     // i cannot use any method defined in the class Localizzazioni
}

Why I cannot write inside the for object.getId() and using it?? In other word how i can access the element contained in the object?? the object is an iterator of type Localizzazioni . The class Localizzazioni  has some method but i cannot use them? why ....

View Replies View Related

Objects Inside Class Declaration

Apr 13, 2014

Ok say you have

public class MyClass {
private int x = 5;
Object myObject = new Object();
public MyClass(){
}
}

When would the myObject be created? Before or after the constructor? And does this mean you can't pass
this into the parameters of that object?

View Replies View Related

How To Access Objects Data From Another Class

Oct 3, 2014

I've been unable to figure out how to access an objects data from another class. I ended up missing a lesson in java and haven't been able to catch up on this topic on my own through my textbook.

Error: has private access

Code:

public class TestCoffeeDrinker {
public static void main(String[] args) {
Coffee latte = new Coffee("Starbucks Tall Latte", 2.85);
Coffee mocha = new Coffee("Starbucks Grande Mocha", 3.95);
Coffee mcdonalds = new Coffee("McDonalds McCafe", 0.99);
System.out.println(mcdonalds.toString());

[Code]...

View Replies View Related

Adding Selected Toppings To Array In Another Class?

Dec 2, 2014

I need to add from what is selected in some check boxes to an array that is in another class, but I can't get the other class to be called correctly.

I need to get the toppings that are selected from the check boxes added into the array that is in the calculations method.

Calculations method

Java Code:

import java.util.ArrayList;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

[Code] .....

View Replies View Related

Swing/AWT/SWT :: Adding Arraylist To Jtable In Another Class

Jan 23, 2015

I'm trying to fill my jtable with an arraylist. The problem is the jtable is in an extended class and the arraylist in the mainGUI. Now how can I fill the jtable with the arraylist?

That's the arraylist in my MainGUI

BufferedReader in = null;
ArrayList<String> data = new ArrayList<String>();
try {
in = new BufferedReader(new FileReader("1.dat.txt"));
String str;
while ((str = in.readLine()) != null) {
data.add(str);

[Code] ....

View Replies View Related

How Can One Create Array Of Objects Of Class Type

Feb 10, 2015

I'm really new to object/class concepts and already having difficulties with applying them. How to create and return an array of Exam objects? I need to get a data from a textfile which is passed to the method.

Java Code:

public Exam(String firstName, String lastName, int ID, String examType, int score) {
this.firstName = firstName;
this.lastName = lastName;
this.ID = ID;
this.examType = examType;
this.score = score;

[Code] ....

View Replies View Related

How To Create Array Of Objects Of Class Type

Feb 10, 2015

I'm really new to object/class concepts and already having difficulties with applying them. how to create and return an array of Exam objects? I need to get a data from a textfile which is passed to the method.

public Exam(String firstName, String lastName, int ID, String examType, int score)

{
this.firstName = firstName;
this.lastName = lastName;
this.ID = ID;
this.examType = examType;
this.score = score;

[code]....

View Replies View Related

Home For Collection Of Objects Of Custom Class

Mar 31, 2015

Where should I keep a collection of instances of my custom class? In the class itself in a static variable?

class Item {
int quantity;
static ArrayList<Item> list = new ArrayList<Item>();
Item(int q) {
quantity = q;
list.add(this);
}
// Some methods and whatnot.
}

Is it fine like this or should I implement the collection elsewhere? What say you?

View Replies View Related







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