How To Sort TreeSet By Age Of Person

Aug 2, 2014

public class Person implements Comparable<Person> {
// the age of the person
private int age;
//the name of the person
private String name;
//the Integer object to wrap the age value;
private Integer ageWrap;

[Code] .....

The collection library has a class named TreeSet, which is an example of a sorted set. Elements in this set are kept in order. Carefully read the description of this class, and then write a class Person that can be inserted into a TreeSet, which will then sort the Person objects by age.

This is the exercise I am trying to solve. And this is as far as I have gotten to. Is it possible to sort my setOfPersons TreeSet directly? I tried to use

Collections.sort(setOfPersons)

method but it wont compile, and I realized that it is not applicable to TreeSet. So I made the

sortByAge()

method to do it indirectly...

I am puzzled though because in the exercise it states

write a class Person that can be inserted into a TreeSet, which will then sort the Person objects by age.

meaning that the TreeSet will sort the Person Objects and not my class..

View Replies


ADVERTISEMENT

Reading From File And Sort Data Based On Person GPA - Catching Error

Dec 9, 2014

I am catching an error in my driver class that reads from a file and sorts the data based on a person's GPA. Here is my code:

import java.io.*;
import java.util.*;
public class Driver {
public static void main(String[] args) {
new Driver(args[0]);

[Code] ....

Why throwing this exception?

View Replies View Related

Why Not Getting All The Elements In TreeSet

Aug 1, 2014

public void createPersons() {
Random rand = new Random();
for(int index = 0; index < persons.length; index++) {
Person person = new Person(rand.nextInt(maxAge), persons[index]);
setOfPersons.add(person);

[Code] ....

setOfPersons is a TreeSet, persons[] is an array of strings that contains 10 names. Why printPersons() prints only the half names of setOfPersons?

Note. I do get the size of the TreeSet as 10...

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

Tokenize A String - Removing Numeric From A TreeSet

Nov 23, 2014

I am using a TreeSet to tokenize a string. The output is sorted with numeric first followed by words

E.g. 13 26 45 and before etc.....................

Is there a tidy way to remove the numeric?

Last bit of my code is :-

// print the words separating them with a space
for(String word : words) {
System.out.print(word + " ");
}
} catch (FileNotFoundException fnfe) {
System.err.println("Cannot read the input file - pass a valid file name");
}

View Replies View Related

Next Birthday Of Person - Days Remaining Total Way Off?

Oct 2, 2014

I'm supposed to calculate and display the number of total days, and then the number of months, and days from now (when the program is run) to when the person's next birthday is (no leap year). Here is my code:

import java.util.Scanner;
import java.text.*;
import java.lang.System;
import java.util.Date;
import java.util.Calendar;
public class CSCD210Lab3 {
public static void main (String [] args)

[Code] .....

When I call daysFromMillis in the print statement, if I enter in a birthday of 02/17/1993, I will get an answer of "our birthday is 5435 days from now."

View Replies View Related

Writing A Program To Test Person Class

Nov 4, 2014

User-defined classes. The concept of getters and setters goes right over my head. Right now we're to write a program to test the Person class below. We have to create 2 instances of the class to test each constructor, and test each method.

class Person {
// Data Members
private String name; // The name of this person
private int age; // The age of this person
private char gender; // The gender of this person
// Default constructor
public Person() {
this("Not Given", 0, 'U');

[code]....

then my output will print out the name. But the assignment doesn't tell us to modify the Person class at all, just to create a tester one.

View Replies View Related

Program That Verify The Correctness Of Person Birth Date

Mar 9, 2015

How to write a program that will verify the correctness of a person`s birth date in java. The birth date is to be typed in by the user. the program should verify the following: Birth date entered should be of type java.util.Date(), Birth date may not be in future, Age may not exceed 110 years.

View Replies View Related

Program Should Copy Person With Highest Percent From File

Mar 23, 2014

The program should copy person with the highest percent from the file 'plik1', and then put it to the file 'plik2'.The problem occurs while compiling.

Java Code: string cannot be resolved to a type
string cannot be resolved to a type
string cannot be resolved to a type
string cannot be resolved to a type
string cannot be resolved to a type
string cannot be resolved to a type
string cannot be resolved to a type
string cannot be resolved to a type mh_sh_highlight_all('java'); Code:

[code]....

View Replies View Related

Calculate And Display Person Body Mass Index

Feb 12, 2015

I have to write a program that calculates and displays a person's body mass index (BMI). The BMI is often used to determine whether a person with sedentary lifestyle is overweight or underweight for his or her height. A person's BMI is calaculated with the following formula:

BMI = Weight X 703/Height^2

where weight is measured in pounds and height is measured in inches. The program should display a message indicating whether the person has optimal weight, is underweight, or is overweight. A sedentary person's weight is considered optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered underweight. If the BMI value is greater than 25, the person is considered overweight.However I can't seem to get rid of these errors:

----jGRASP exec: javac BMI.java
BMI.java:28: error: cannot find symbol
Scanner keyboard = new Scanner(System.in);
^
symbol: class Scanner
location: class BMI
BMI.java:28: error: cannot find symbol

[code]....

View Replies View Related

Java Class Person - Adding Data To Text

May 5, 2014

Your Tester class main method must use an array or an ArrayList of Person objects. It must be populated from a text data file, friends_data.txt. Add the following data to your text file,

Michelle, 12/20/2008, Camilla
Bryan, 3/8/2007, Tom
Camilla, 6/7/2005, Michelle
Tom, 10/15/2007, Bryan
Charlotte, 3/2/2008, Michelle

Each line has the meaning:

-Person name, Person date of birth (MM/DD/YYYY), name of best friend
-Write a Java program that reads in the data from the friends_data.txt file, allocates a new
-Person for each and then stores the newly created object in either an Array or an ArrayList.
-Next print out a report showing the following information for each Person,

1. The Person's name
2. Their popularity counter
3. Their age on May 1, 2014
4. The name of their best friend
5. The age of their best friend on May 1, 2014

Finally, print the name of the most popular Person and the name of the oldest Person.

Person Class

import java.util.ArrayList;
public class Person {
public String personsName;
public String personsFriend;
public String personsBirthday;
public int personsPopularity;
public int popularity = 0;

[code]...

I keep getting this error from the compiler:

System.out.println("Popularity : " + personsOfInterest[i].getPopularityNumber());

"method getPopularityNumber in class Person cannot be applied to given type:
Required: java.lang.String[]; found: no arguments; reason: actual and formal argument lists differ in length.

View Replies View Related

GUI Application - Read Some Person Details From A File And Then Put The Result In JList

Aug 31, 2014

I'm new to programming and java and I'm trying to understand how exactly this piece of code works and what exactly does. I'm trying to make my first GUI application and to read some person details from a file and then put the result in a Jlist.

private static final String SEPARATOR = ",";
public static ArrayList<Speaker> getAllMembers() {
ArrayList<Member> members = new ArrayList<Member>();
try {
File file = new File(Resources.MEMBERS_TXT);
Scanner fileReader = new Scanner(file, "utf-8");
String[] properties;

[Code] ....

View Replies View Related

Calculate Person Calories Burned Per Minute - Possible Loss Of Precision Error

Feb 2, 2015

I was asked to write code to calculate a person's calories burned/min. This is what I got. The problem is I keep getting an error.

--------------------Configuration: <Default>--------------------
C:Program FilesJavajdk1.7.0_72CaloriesBurned.java:22: error: possible loss of precision
caloriesBurnedPerMinute = 0.0175 * METS * weightInKg;
^
required: int
found: double
1 error

Loss of precision? What does that mean? Do I have to change weightInKg into some other number type?

import java.util.Scanner;
public class CaloriesBurned
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int runningHours, basketballHours, sleepingHours, METS, caloriesBurnedPerMinute;

[Code] ....

View Replies View Related

Web Services :: Telephone Book - Enter Phone Number / Correct Person With Name And Address Should Appear

Nov 30, 2014

Applications: Netbeans (8.0.1), Apache Tomcat

My Aim: Creation of a telephone book

How should it work:

I have a Database with Data of people (name, adress, phone number).

If a user enters a phone number (CLIENT) -> the correct person with the name and adress should appear.

I build up the infrastructure and now I'm at the point that I have to create a Webmethod for it. I just worked on projects like addition of two integers, hello "name", ... I think this example now is harder, BUT i can learn a lot!

View Replies View Related

App That Declares A Class Person (String Name / Int Age) And Account Class?

Mar 31, 2014

I want to write an app that declares a class Person(String name, int age), and an Account class, Account(int code, double balance).But, additionally, every Person has at most 3 accounts, and each account has a Peron associated with it.

my code so far...

public class Person {
private String name;
private int age;
private Account[] accounts;
private int numOfAccounts;
public Person(String name,int age){
this.name=name;

[code]....

My problem is:When I make data input for a person, and additionally I want to read data for the account(s) that this rerson has, what code should I write to create a new Account object as account[numOfAccounts].And, what is the code to assign an owner to a new created Account object?

There exists a relationship between the two classes, but I cannot find the way to implement this relation....

View Replies View Related

Sending To All Vs Sending To One Person In Server / Client Program

Apr 17, 2014

I have two programs that I'll post below, one is a Server and the other is a Client. The ChatServer runs once and many clients can run ChatClient from separate computers and talk together in their GUI's. The the client program comes with two buttons, one that's simulates the sending of a message to a single user ; "Send Message To User", and one that just generally sends a message ; "Send Message To All". Although now the Clients on the server seem to be able to selectively send messages to an individual by giving the recipient name, all the clients can see that message, which is not exact what I am aiming for. What I am going for is having the "Send Message To User" button click to send a message to the named user without all other users seeing the message.

The Server Program Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

[Code] ....

Now I have tried thing like having various input output streams and trying to connect those, but no luck. Tried fiddling with having the names arraylist directing the messages to one client versus all but that did not work out either. How I what I would need to do to go about doing this?

View Replies View Related

How To Sort Jtable

Jan 9, 2014

how can i sort a Jtable, If i have in a certain column Strings and Integers, but I don't want to sort like string.example:

ID |
----------
1
11
2
213
22
MOSTR
MOSTIR

i dont want this to happen when i sort this column, I'm using table.setAutoCreateRowSorter(true);

View Replies View Related

How To Sort ArrayList

Jun 30, 2014

How can i sort my ArrayList, which contains cars, with year and used year, i want to sort them first from year, and then from used year . what should i use?

View Replies View Related

How To Sort A List By Balance

Mar 26, 2014

Add accounts on a list, each account contain: name, accountCode, pinCode, balance.

How to show list sort by balance?

View Replies View Related

Sort String Method

Aug 7, 2014

I am getting incombatable types, I do not know why I am getting them..why I am getting the error?

The Error I am getting:
stringSort.java:26: error: incompatible types
if(myArray[j].compareToIgnoreCase(myArray[i].toString())){
^
required: boolean
found: int
*/

[code]....

View Replies View Related

Recursive Selection Sort

Oct 21, 2014

How would I modify this version of a selection sort into a recursive method?

public static void selectionSortRecursive(Comparable [] list, int n)
{
int min;
Comparable temp;

[code]....

View Replies View Related

Selection Sort Method?

Mar 5, 2014

I'm trying to make a selection sort method that will sort a list of Strings alphabetically.

I have a test list of Strings like so:

Joe, Steve, Oscar, Drew, Evan, Brian, Ryan, Adam, Zachary, Jane, Doe, Susan, Fred, Billy-Bob, Cindy, Mildred, Joseph, Hammer, Hank, Dennis, Barbara

However, whenever I run the method, the element that should go last, Zachary, in this case, ends up getting moved to the front for some reason. I'm not sure why.

I tried changing what the first element was initialized to, to the variable i as that would logically work as well, but it ends up missing the first element in the list.

Java Code:

public static void selectionStringAscendingSort (String[] words){
int i, j, first;
String temp;
for ( i = 1; i < words.length; i++ ) {
first = 0; //initialize to subscript of first element
for(j = i; j < words.length; j ++){ //locate smallest element between positions 1 and i.
if( words[ j ].compareTo(words[ first ]) <0 )
first = j;
}
temp = words[ first ]; //swap smallest found with element in position i.
words[ first ] = words[ i ];
words[ i ] = temp;
System.out.println(Arrays.toString(words));
}
System.out.println(Arrays.toString(words));
} mh_sh_highlight_all('java');

View Replies View Related

Insertion Sort Using ArrayList

Sep 27, 2014

Im trying to do an insertion sort using ArrayLists and I keep getting this error after the sorting section where it doesnt sort anything at all, but still displays my previous array list.:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Utilities.insertionSort(Utilities.java:102)
at Utilities.main(Utilities.java:66)

My code:

import java.util.*;
import java.lang.*;
public class Utilities {
public static void main(String[] args) {
ArrayList<String> equipment = new ArrayList<String>();

[Code] ......

View Replies View Related

Sort Java Array With More Than One Value?

Jan 22, 2015

how can I got about sorting an array that contains more than one value in a single element. Such as my array below has 4 values under one element. I know how to sort elements with single values however, slightly confused on this.

import java.util.Scanner;
import java.util.Arrays;
class Mobile
{

[Code]......

View Replies View Related

Sort A Set Of Parallel Arrays

Mar 30, 2014

I am trying to sort a set of parallel arrays. I really believe that the code is correct, but it is not working out as expected.This is the specific code for the sort:

Java Code: for (int y = 1; y < (dataArray.length + 1); y++)
{
for (int x = 0; x < dataArray.length - 1 ; x++)
{
if ((dataArray[x][1]) <= (dataArray[x + 1][1]));
{
tempOpen = dataArray[x][1];
dataArray[x][1] = dataArray[x + 1][1];
dataArray[x + 1][1] = tempOpen;

[code]....

View Replies View Related

How To Sort Names Using Compareto

Feb 24, 2014

i have this problem with my code. it need to put three names in alphabetical order that are entered from the user. then output the alphabetical result. the program compiles but when you put in different names there are not alphabeticals. i think only the first if works good.

import javax.swing.JOptionPane;
public class Sort
{
public static void main(String[] args)
{
String name1;
String name2;
String name3;
 
[code]...

View Replies View Related







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