Dealing With Presorted Object Array

Apr 11, 2014

I am trying to solve involves an object array each object has two instance variables, a string(origin) and a double(price), that was originally unsorted I have already sorted the array. There are a total of 11 possible origins and 6 prices, so obviously some origins share prices with others. The array is sorted by origin alphabetically ignoring case. I now have to write to a file how many instances of each origin there is, tally up all the prices for each origin and a total for all prices. I get bonus points for utilizing the presorted array to my advantage for the final output and calculations.

It would be easy to write a branching statement and tally up all the origins and prices etc... However this is a linear operation and doesn't seem to benefit from the presorted array at all so I came up with what I think is a descent alternate solution.

I was thinking that I could go through the array one object at a time and stop when I came to an origin of the next type. For example: there are 12 names called "Adam" and then a name called "Bob". So if I stop at bob, check the index and add 1 then I know how many Adams there were. I can then continue this process with all the other names, all the while calculating the combined prices and all.

View Replies


ADVERTISEMENT

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

Jsoup And Dealing With File Extensions

Oct 4, 2014

I am working with jsoup right now and I am trying to get a range of file extensions such as doc, docx, txt, pdf, and so on. Anyways i have looked through the jsoup api and cant seem to find what I am looking for. The closest thing I have found is

Elements files = doc.select("a[href$=.doc]");

This should work but only grabs one extension. How to grab multiple at one time? My guess would be:

Elements files = doc.select("a[href$=.doc]").select("a[href$=.docx]").select("a[href$=.txt]");

However I'm not sure for certain. Anyways I will provide a link for the api .....

View Replies View Related

Spanning Tree - Dealing With Static Variables

Feb 20, 2014

I'm new to Java and I'm trying to create a spanning tree in the desired order with 4 nodes (0,1,2,3). I've got the following code:

import java.util.ArrayList;
public class A {
public static boolean any(ArrayList<Integer> input) //To check for if any element in the ArrayList == 1
{
boolean answer = false;
for(int i=0;i<input.size();i++) {
if(input.get(i)==1)

[Code] ....

What happens is that the input parameter adj and hence the original adjmat inside main gets changed everytime I enter the method "connected", and I don't want this to happen. I understand that this is due to the main method being static and hence adjmat becomes static as well, but how do I change the code such that adjmat only gets modified after coming out of the connected function, and not while inside it?

View Replies View Related

Java Dealing 5 Random Cards From A Deck

Aug 3, 2014

I redid my entire code to use array-lists instead of just arrays my professor finally got back to me and said he doesn't want us to use lists. The assignment is to create a CardGame driver class, then create a "card" in a card class, then a "deck of cards" in a DeckOfCards class, shuffle, and deal 5 random cards and "deal a card and report the number of cards left in the deck". That last line in quotes is what I do not know how to do.

Also, I renamed a lot of variables via some suggestions and the assignments states "Make sure to write the appropriate constructors, getters, setters, toString and other methods as required for both classes." I think they're appropriate. Should I change some of he methods to better getters and setters identifiers?

Here is my code.

import java.util.Random;
public class CardsGameTest {
//execute application.
public static void main(String[] args) {
DeckOfCards myDeck = new DeckOfCards();
myDeck.shuffle(); //shuffle cards.

[code].....

The instructions also say to "print each card as it is dealt" does that mean 5 cards one at a time? Anyways, I was thinking that in the driver class I could add a for loop to the for loop and as it deals a card it could run through the second for loop and print how many cards are left in the deck.

View Replies View Related

Dealing With Spaces During Input String Conversion?

Oct 5, 2014

I am working on a small brain teaser project where I am taking a string input from a Scanner, and turning into ascii. The problem comes into play when the string has a space in it, so if the question is what's your name? and you say Michael Jackson, Michael gets converted then Jackson becomes the answer to the next question, rather then the second portion of the current string.

This is an older version of what I'm doing currently, but it had the same basic problem with spaces.I will say I did my current version entirely different.

nner user_input = new Scanner (System.in);
//Creates a string
String favoriteFlick;
System.out.println("Enter the title of your favorite film?");
favoriteFlick = user_input.next();

[Code] .....

View Replies View Related

Setters And Getters In Java Program - Dealing With Circles

Oct 29, 2014

You should write a class that represents a circle object and includes the following:

1. Private class variables that store the radius and centre coordinates of the object.
2. Constructors to create circle objects with nothing supplied, with just a radius value supplied and with a radius and centre coordinates supplied.
3. Public instance methods that allow the radius and centre coordinates to be set and retrieved (often known as set/get methods).
4. Public instance methods that return the circumference and area of the circle.
5. A public class method that tests if two circle objects overlap or not

Here is my code:

import java.lang.Math;
public class Circle {
private double xCentre, yCentre, Radius;
// constructors
public Circle() {
xCentre = 0.0;
yCentre = 0.0;
Radius = 1.0;

[Code] ....

The second program just has to demonstrate each aspect addressed in the brief I pasted at the top.s of what else I can do.

This is as far as I got during the 3 hour lab session I had and both compiled fine but when running just displayed all the text eg. "Circumference of first circle is ", but didn't display any numeric values. I don't have the facilities to actually run the program unless I'm in the computer lab, I have a short opportunity to go in tomorrow but that will be the last so I'm doubtful that I'll get it fully working in time.

The problem is that when this code runs it doesn't display any numerical values, ie nothing is being passed between the two programs.

View Replies View Related

Add Object To Array

Feb 28, 2014

I need to add an employee object that my program has created and put it in a array list. My employee object is first name, last name, and a generated id. I am trying to add a new employee object to an array like this:

public static Employee addEmployees(String first,String last) //this method is in my employee class
{
Employee employee = new Employee(firstName,lastName);
employee.setFirstName(firstName);
employee.setLastName(lastName);
employee.getId();
employeefile.addEntry(employee); //add employee object to array list

[code]...

how to println it to test, but I can't seem to do that or know which class to put this function in. I know about for loops and such, just don't know how to implement it or where.

View Replies View Related

Getting String Object Into One Array And Then Into Another

Sep 28, 2014

this is part of a larger project but i figure if i can figure out this first step i can work the rest. I need to get a title from the user add it to a object array (i think thats what it is) and then when i call listAllItems(), items[] should be copied into a string array called listAllItems, and then printed, but currently im jusst having trouble i think making it an object and then make it an item in the items[], if i run case 3 the list it says null...

public class MediaItem {
String title;
MediaItem(){
}
MediaItem(String title){

[Code] ......

View Replies View Related

Java Deck Of Cards And Dealing Five Cards

Aug 2, 2014

Basically I started writing my code in one class, then I split it up into a Card class and a DeckOfCards class and I now need to figure out how to get it all to work together. I get a little confused with calling methods sometimes, especially when separate classes are in play. I think I just need a method to deal out . Besides getting it all working together correctly. I'm also having trouble creating the method to deal out five cards that also tells how many cards are left in the deck and I believe I need a toString method but I honestly do not know how to go about that.

Design and implement a class called Card that represents a standard playing card. Each card has a suit and a face value. Then create a class called DeckOfCards that stores 52 objects of the Card class. Include methods to shuffle the deck, deal a card and report the number of cards left in the deck. The shuffle methods should assume a full deck. Create a driver class (CardsGame) with a main method that deals five cards from the shuffled deck, printing each card as it is dealt. Make sure to write the appropriate constructors, getters, setters, toString and other methods as required for both classes.

The main class, CardsGame Class

import java.util.Scanner;
public class CardsGame {
public static void main (String [] args) {
DeckOfCards deck = new DeckOfCards();
//call shuffle
deck.shuffle();

[Code] ....

View Replies View Related

What Is The Length Of A Null Array Object

Feb 21, 2014

I tried the following and it gives me null pointer exception. I just want to verify if what is the actual value of a length of an array?

Object [] obj = null;
System.out.println("length:" + obj.length);

View Replies View Related

Java Object Stored In Array?

Apr 8, 2014

I have been having trouble with a recent hw assignment requiring us to sort user defined data types in an array. so far, i have implemented the sorting algorithms correctly but i cant seem to store a string and an integer together. I have an employee class

[public class employee {
private String Name;
private int IdNumber;
public employee(int IdNumber,String Name){
this.Name=Name;
this.IdNumber=IdNumber;}

[Code] .....

And in my app, i create a employee object-----> employee john=new employee(1020, "John");

How can I prompt my insert method to take in employee id and string name together?

View Replies View Related

Copy ArrayList Object Into Array

May 29, 2014

package com.practice;
public class Car {
private String name; //name of the car
private String modelName; //Name of the model
private int year; //The year car was made in
private int speed=0;

[Code] ...

It wont let me copy it into a array is there any solution to this.

View Replies View Related

Return Array Contain All Object Name Of A Class

Mar 16, 2014

I need to return all the object name of one class in an array. I have a class named country, and other classes with athletes and medals etc. I need to do a method that list an array with all the countries that participate, so all the objects created with the class country (i.e canada.country, usa.country, etc). Is there a way I can retrieve them?

View Replies View Related

Return Array Containing All The Object Name Of A Class

Mar 16, 2014

I need to return all the object name of one class in an array. I have a class named country, and other classes with athletes and medals etc. I need to do a method that list an array with all the countries that participate, so all the objects created with the class country (i.e canada.country, usa.country, etc). Is there a way I can retrieve them?

View Replies View Related

Making Object And Adding To Array

Oct 2, 2014

I need to be able to call this method and it should take the object and, add it to my list of items, but I am having trouble getting it to work. I know the numbers and stuff aren't correct eventually it will add one to the array length when i call add item, but I am just trying to go one step at a time

//creates new MediaItem object and add it to items[]
void addNewItem(String title){
MediaItem object = new MediaItem(title);
MediaItem[] items = new MediaItem[1];
items[0] = ("object.getTitle()");
numberOfItems = numberOfItems + 1;
System.out.println(items[0]);

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

Array List That Contain String Object

May 5, 2014

Suppose that you have an ArrayList and that it contains String objects. Which declaration of the ArrayList requires that objects retrieved using the get method be cast to Strings before calling a String method?

I. ArrayList a = new ArrayList();
II. ArrayList<Object> a = new ArrayList<Object>;
III. ArrayList<String> a = new ArrayList<String>;

A. I only
B. II only
C. III only
D. I and II only
E. I, II, and III

I know that all of these are ways to declare an Array List, but I am unfamiliar with the last two since I usually just declare my Array Lists with the first option.

View Replies View Related

Filling Array With Object - Deck Is Empty

Jun 6, 2014

I am trying to create an array filled with the object Card. It keep throwing the exception that the "deck is empty". I am not sure why that's happening because the code for filling the array seems fine.

public class Deck {
private Card[] deck;
private CardPile cardPile;
private int numCards;
public Deck() throws InvalidDataException{
this.deck = new Card[52];

[Code] .....

View Replies View Related

Create Country Object And Store In Array

Oct 12, 2014

Write a program that prompts the user for information about some countries, creates an object for each country, and then stores the objects in an array. After the user has entered information about all the countries, your program should print out which countries in the list have the smallest and largest area and population density. Assume the user will enter information about at least one country but that the program will not have to store more than ten countries in the array. The user will indicate that they are done entering countries by typing "DONE" for a country name. Here is an example of what your program must look like when it is executed (user input is shown bold)

Please enter the name of a country: United States
Enter the area in square km and population of United States: 9827000 310000000
Please enter the name of a country: Mexico
Enter the area in square km and population of Mexico: 1973000 122300000
Please enter the name of a country: Canada
Enter the area in square km and population of Canada: 9985000 35160000
Please enter the name of a country: Liberia
Enter the area in square km and population of Liberia: 111370 4294000
Please enter the name of a country: DONE

Liberia has the smallest area at 111370 square km.
Canada has the largest area at 9985000 square km.
Canada has the smallest population density at 3.521282 people per square km.
Mexico has the largest population density at 61.986822 people per square km.

I don't know, I'm really confused with this Java program. I don't know how to do the main part of the program where the user inputs, array, and the output.

// Access the Scanner and ArrayList class by importing the java.util package.
import java.util.Scanner;

/** Project - A Country Object
* The purpose of this program is for the user to enter some information at most 10 countries. After the user has entered information about all the countries, the program should print out which countries in the list have the smallest and largest area and population density.
*/

public class Country
{
private String countryName;
private int theArea;
private int thePopulation;
public Country(String countryName, int thePopulation, int theArea)

[Code] ....

View Replies View Related

Parse Value Of Array Of Object Passed Through A Parameter

Oct 29, 2014

I'm working on a method that would parse the value of the array of object that I passed through a parameter. I would like to ask if making Object as a parameter is doable. Let's say I have a class Student and Teacher. I created a class the would handle the sched and name it class Schedule and extend this class to the Student and Teacher. Now I want to have a function that will accept an array of Schedule from either Student and Teacher, what ever object I will pass in the parameter. I know its easy to just make a method with a separate parameter of my classes but im looking for a more dynamic code.

class Student extends Schedule{
//variables here for student
}
[code]

class Teacher extends Schedule{
//variables here for teacher
}
[/code]

private void parseObject(ArrayList<Object> objct){
Schedule temp = objct.get(0);
//there is no error in this part

}

Now when i will try to use the function and pass a data, it will not accept since my parameter should be an array of object. How would I twist dis one?

ArrayList<Student> temp_student = new Array....
parseObject(temp_student); // it will not accept my parameter, how would i make it as an object

View Replies View Related

Constructor That Creates A New Object With Data From Array Values?

Feb 11, 2015

So I want to write a constructor that creates a new object with the data from the array values. I don't know where to start. It's the last method in the code:

public class Measurements {
private double[] values;
private double[] newArray;
private int n; //numberofvalues
private double[] ms;
public Measurements(int max) { //constructor

[code]....

View Replies View Related

Method That Returns Largest Object In Array Of Objects

Apr 4, 2015

This is what I have to create : Write a method that returns the largest object in an array of objects. The method signature is:

public static Object max(java.lang.Comparable[] a)

All the objects are instances of the java.lang.Comparable interface. The order of the objects in the array is determined using the compareTo method.

Write a test program in the main method that creates an array of ten Strings, an array of ten Integers, and an array of ten java.util.Dates, and finds the largest string (i.e. in the alphabetical order), Integer, and Date in the arrays.

Name your java class Max and your java file Max.java.

I am struggling a bit with this code as I am sure you can see, and am at loss. I have never used the compareTo method. Am I doing this right, or on the right track with my code?

public class Max implements Comparable {
public static Object max(java.lang.Comparable[] a) {
java.lang.Comparable tempObj = null;
for (int i = 0; i < a.length; i++) {
if (a[i].compareTo(tempObj) > 0)

[Code] ....

View Replies View Related

Cannot Find Symbol Class Object When Trying To Initialize Array?

Sep 9, 2014

I'm trying to make an array of objects, which I then initialize using objects that I have already created. I have main class, a secondary "Other" class, and a third "Other2" class. In the first Other class, I create three objects of the Other2 class, an object array of the type Other2 , and I then try to add the three objects to the object array, which results in the errors:cannot find symbol: class objectArray, ] expected, identifier expected

here is my code:

Other class
public class Other
{
Other2 object1 = new Other2();
Other2 object2 = new Other2();
Other2 object3 = new Other2();

[code]....

View Replies View Related

Deck Of Cards And Dealing Five Cards

Aug 2, 2014

I have an assignment for my summer class. Basically I started writing my code in one class, then I split it up into a Card class and a DeckOfCards class and I now need to figure out how to get it all to work together. I get a little confused with calling methods sometimes, especially when separate classes are in play. I think I just need a method to deal out . Besides getting it all working together correctly. I'm also having trouble creating the method to deal out five cards that also tells how many cards are left in the deck and I believe I need a toString method but I honestly do not know how to go about that. FYI, I think the prof would rather arrays then enums since we're dealing with arrays right now hence the array for the 52 card deck.

Here are the directions...

Design and implement a class called Card that represents a standard playing card. Each card has a suit and a face value. Then create a class called DeckOfCards that stores 52 objects of the Card class. Include methods to shuffle the deck, deal a card and report the number of cards left in the deck. The shuffle methods should assume a full deck. Create a driver class (CardsGame) with a main method that deals five cards from the shuffled deck, printing each card as it is dealt. Make sure to write the appropriate constructors, getters, setters, toString and other methods as required for both classes.

The main class, CardsGame Class

Java Code:

import java.util.Scanner;
public class CardsGame {
public static void main (String [] args) {
DeckOfCards deck = new DeckOfCards();
//call shuffle
deck.shuffle();

[code]...

View Replies View Related

Public Method That Takes Array Of Type Object To Load Strings Into Linked List

Oct 13, 2014

I am having a little trouble with a part of my Java assignment that needs to have linked lists in it.

I need to write a public method that takes an array of type object to load strings into a linked list.

View Replies View Related







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