Sorting ArrayList Of Geometric Objects By Area From Smallest To Largest
Mar 18, 2014
I have to create a method with the following header :
public static <E extends Comparable<E> > void sort ( ArrayList<E> list , int left, int right)
i also had to create a swap cells method and position of max integer method. and also had to read the preserved data file in with a scanner. I implemented the comparable interface I am having difficulty sorting my list by the area. It has to be in descending order.
Geometric Object class: since it has comparator also am interested if i need to change this?
CODE:
Driver:
public static void main(String[] args) throws IOException, ClassNotFoundException {
Circle c1 = new Circle (4, "red", false);
Circle c2 = new Circle (2, "blue", true);
Circle c3 = new Circle (10, "blue", true);
Rectangle r1 = new Rectangle (10, 6, "yellow", true);
Rectangle r2 = new Rectangle ( 5, 11, "green", true);
ArrayList <GeometricObject> list = new ArrayList();
[Code] ....
View Replies
ADVERTISEMENT
Oct 14, 2014
I have a requirement to find the greatest/maximum of the given list of Calendar objects in Java.
i.e., 2013/01/26
2014/03/03
2012/02/27
2014/01/15
So the above list of calendar objects are in Arraylist. I need to get the max/greatest among these. Eventually, the answer should be 2014/03/03
View Replies
View Related
Nov 15, 2014
I am trying to display Largest value and Smallest value in an array but when I execute I get Largest value as 0.
Following is the code:
package simpleArray;
import javax.swing.JOptionPane;
class ArrayInteractive {
static String ch = "Q";
static boolean flag = true;
static String a[];
[code]....
View Replies
View Related
Apr 9, 2015
Write a program that finds both the largest and smallest of several integers. T. For example, a program that determines the winner and loser of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest; the salesperson who sells the least units loses the contest. Write a pseudocode program, then a Java program that inputs a series of 10 integers and determines and prints the largest integer and the smallest integer. Your program should use at least the following four variables: counter, number , largest and smallest. After i run the code i just get the largest input without the smallest .
This is my code :
import java.util.Scanner; // program uses scanner
// main Class begins
public class Largest12
{
public static void main(String[] args) //main method begins
{
//Initial variables
int counter = 0; // initiate counter
int number=0 ;
[Code] ....
View Replies
View Related
Jan 20, 2015
I want to reverse grades that i have put using JOptionPane from largest to smallest and the corresponding names..
import javax.swing.*;
import java.util.Arrays;
public class ArrayUtils {
public static void main (String[]args){
String length = JOptionPane.showInputDialog("Number of students");
[code]....
View Replies
View Related
Nov 19, 2014
I know it is possible to do this using the Array class but I don't want to use that. I am trying to use selection or bubble sort but I believe selection is what I want. This program asks the user to input 2 arrays with 10 integers each, sorts them from smallest to largest, then states whether they are the same or not. Comparing the two arrays.
import java.util.Scanner;
public class SetDifference
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
int array1[] = new int[10];
int array2[] = new int[10];
[Code] .....
View Replies
View Related
Jan 13, 2015
how can i make a program that declare the largest and smallest out of three numbers??
/*
* To change this license header, choose License Headers in Project Properties.
int a;
int b;
int c;
[Code]....
View Replies
View Related
Apr 27, 2014
Here is a simple program on applets that is giving me a bit of headache since it cannot always display the two numbers that it is supposed to: the largest and smallest of the five input integers.....I cannot see where i went wrong, you can always create separately the HTML file to load the applet in an appletviewer of your won size.
*
23.6 (Largest and Smallest) Write an applet that reads five integers, determines which are the largest and smallest integers in the group and prints them. Draw the results on the applet.
*/
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
[Code] .....
View Replies
View Related
Apr 15, 2014
I have to get the user to enter in 10 numbers and with those 10 numbers I have to find the total, average, smallest, largest numbers in the set the user inputs. I have the total and average already figured out but how would you go about trying to find the largest and smallest numbers within this set of code.
import java.util.*;
public class testhw7
{
public static void main (String [] args) {
Scannernumberin = new Scanner (System.in);
doublevalue[];
doubletotal;
[Code]...
View Replies
View Related
Feb 18, 2015
Suppose s, t, and w are strings that have already been created inside main. Write a statement or statements, to be added to main, that will determine if the lengths of the three strings are in order by length, smallest to largest. That is, your code should determine if s is strictly shorter than t, and if t is strictly shorter than w. If these conditions hold your code should print (the boolean value) true. If not, your code should print false. (Strictly means: no ties)
View Replies
View Related
Dec 4, 2014
/*
Purpose: To write the methods and the rest program. The program should fill a 4 X 4 2 dimensional array with random numbers between 100 and 200. The program should then determine and print the largest and smallest values within the array using two Methods Largest and Smallest. The program should then determine and print the number of values within the array that are even using a function called Even. The program should also enter a loop that will only terminate when the user inputs a -1 as a choice. The loop should continue to ask the user to guess a number that was randomly generated. The program should call the Findit function to determine if the number was in the array. The program should print out the values in the array when the user selects a -1 and then terminate.
*/
import java.util.Scanner;
import java.util.Random;
public class LNFI_2DArray
{
public static void main(String[] args) {
int guess;
int[] array = new int[4];
[Code] ....
I just had this code working, then all of a sudden i was hit with a 'keyboard leak' error code.
View Replies
View Related
Oct 19, 2014
A java program to print 10 random numbers between 512 and 1024 and then finds the smallest and biggest numbers from the list using only one loop
class BSLab4d {
public static void main (String[] args){
int[] nums = new int[10];
int largest = nums[0];
int x = 0;
int smallest = nums[0];
int y = 0;
[code]....
View Replies
View Related
Oct 29, 2014
I have problem with sorting my numbers from smallest to biggest and i need to not have repeating numbers, I am stuck. I tried using Arrays.sorts(lottery) but didn't work. and i don't know to but make the numbers not repeat in the same line.
package lottonumbers;
public class LottoNumbers {
public static void main(String[] args) {
int[] lottery = new int[6];
for (int numoftimes=0;numoftimes<5;numoftimes++){
[Code] ....
View Replies
View Related
Feb 12, 2014
These are all arraylists and not arrays
I have currently two arrays.
Java Code:
String[] chunks = {"a","b","c"};
int[] freq = {2,4,3}; mh_sh_highlight_all('java');
I am looking for a way to order these from greatest to smallest based on frequency.
The resulting arrays should be:
Java Code:
chunks = {"b","c","a"};
freq = {4,3,2}; mh_sh_highlight_all('java');
Because b is most frequent, and a is least frequent.
One way I can think of doing this is a two dimensional String array
Java Code: String[][] fullHold = {{"b","c","a"},{"4","3","2"}}; mh_sh_highlight_all('java');
Then sort based on the character values of the second row.
How can I sort a two dimensional array based on a single row (where it will rearrange the other rows in accordance).
View Replies
View Related
Mar 24, 2014
I need to create a program that uses ArrayList to store integers the user inputs, then scan the array for the largest number. I would also like the user to be able to exit this loop if the number 0 is entered.
As you can see below, I'm not sure how to correctly exit the do-while loop. I found this on another forum, but it does not work.
Java Code:
import java.util.*;
public class array {
public static void main(String [] args){
ArrayList<Integer> list = new ArrayList<Integer>();
[Code] ....
View Replies
View Related
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
Oct 13, 2014
This assignment requires me to show areas of each shape by using loop. I can do it with abstract and interface , but in this case. I don't know how to use method getArea() to loop for each object
import java.util.ArrayList;
public class TestShape {
ArrayList<Shape> list = new ArrayList<Shape>();
Circle c;
Rectangle r;
Square s;
public TestShape() {
[Code] .....
View Replies
View Related
Nov 26, 2014
I have to sort out my ArrayList but cannot make it work. Here is
Java Code:
Collections.sort(this.users);
User poor = this.users.get(0);
User ritch = this.users.get((this.users.size()-1));
System.out.println("Poorest user has" + poor.getUsername() + poor.getBalance() );
System.out.println("Ritches user has " + ritch.getUsername() + poor.getBalance() + "
"); mh_sh_highlight_all('java');
The Collections.sort(this.users); does not work.
View Replies
View Related
Apr 22, 2015
I'm getting this error:
The operator < is undefined for the argument type(s) java.util.ArrayList<FacebookUser>, java.util.ArrayList<FacebookUser>
For this:
class FriendsComparator implements Comparator<FacebookUser>
{
@Override
public int compare(FacebookUser o1, FacebookUser o2)
{
int returnValue = 0;
if (o1.friends < o2.friends)
returnValue = -1;
[Code] ....
Same as for the second if condition. How can I fix this exactly? What I'm doing is trying to sort Facebook users by the most to least amount of friends.
Here's the Driver:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
[Code] .....
View Replies
View Related
Mar 17, 2014
I am trying to find a concise way to write the sort methods for my class. I am supposed to make a program that can sort objects by a number of fields: year, rank, artist and title.
I used an idea from this thread : java - Sorting a collection of objects - Stack Overflow
And I am trying to use the custom comparator for my sort methods. However for some reason, the sortingBy variable fails to recognize any of the enum types.
Whenever I try to set the sortingBy variable equal to one of them, for example:
Java Code:
private Order sortingBy = Year; mh_sh_highlight_all('java');
I get a "Year cannot be resolved to a variable" error.
What I want to be able to do is make it so every time a specific method is called, say, for example sortTitle(), sortingBy will change to Title, then the SongComparator will sort using the case Title.
Is it possible to do this? I can't figure out how to modify SongComparator's object variables that way.
Java Code:
import java.util.Comparator;
public class SongComparator implements Comparator<Song> {
public enum Order {Year, Rank, Artist, Title}
public Order sortingBy;
[Code] .....
View Replies
View Related
Feb 14, 2015
An array has objects with String firstName, String lastName, int ID, char examType('M' or 'F'), and int score. Every object has both Midterm('M') and Final('F'). The array has to be sorted by the first Midterm- 'M' and immediately followed by the Final ('F') of the same person (object). Im having troubles with coming up with a proper algorithm with gettin the Final- 'F' after Midterm for the same person.
Java Code: public static Exam[] collateExams(Exam[] exams) {
Exam [] r = new Exam[10];
r = exams;
int[] position = new int[10];
int index = 0;
for(int i = 0; i < exams.length; i++)
[Code]...
View Replies
View Related
Mar 30, 2014
I have a PriorityQueue called incoming, containing objects of type Vehicle.
All vehicles have a fuel level, that can be accessed by the getter method getFuelLevel().
I want to be able to choose to sort the queue so that the Vehicles with the lowest fuel levels are at the front of the queue.
View Replies
View Related
Oct 13, 2014
we have an Arraylist<Tweet>, and this class is defined as followe: public class Tweet implements Comparable<Tweet>, Serializable. now if we implement the method comparteTo, then will it be sorted automatically? I want to sort this array by any insert.
View Replies
View Related
Feb 11, 2014
I have a case in which I want to sort two types of ArrayLists (using quicksort) and the method originally coded only accepts a String ArrayList. The problem is that now I want to sort an ArrayList of type int but couldn't . . . so I decided to overload the method. Since it looks very ugly to copy and paste the same chunk of code only to change the method signature I wondered if there is a better way to make this method more dynamic and be able to take in different types of ArrayLists.
My code:
private ArrayList<String> sort(ArrayList<String> ar, int lo, int hi){
if (lo < hi){
int splitPoint = partition(ar, lo, hi);
sort(ar, lo, splitPoint);
sort(ar, splitPoint +1, hi);
[Code] .....
View Replies
View Related
Apr 8, 2014
I have a school assignment that involves me sorting an array of objects based on one of the class String variables. I am using as the title says a simple selection sort method. The problem I'm having is that when I run the program in debug mode, it never seems to enter the if statement in the inner loop. I would like to say I've tried a number of things to figure it out, but honestly I'm just stumped as to why it's not working.
Here is the code:
public static void sortTransactions(Transaction[] oTransaction){// This is the sorting method, obviously it's not done so it currently just prints to screen.
System.out.println("Successful call to sortTransaction()");
String min = "";
int curInd = 0;
Transaction[] temp = new Transaction[1];
[Code] ....
The output when I check to see if the array is sorted verifies that the array never does get sorted.
View Replies
View Related
Mar 15, 2015
I have a ArrayList of objects of class called HockeyPlayer (ArrayList<HockeyPlayer>). A HockeyPlayer has a String name and int number of goals.
This is my current work for comparing each object in the list to every other and printing them to screen:
Iterator<HockeyPlayer> it = hockeyPlayersList.iterator();
while (it.hasNext()) {
HockeyPlayer singleHockeyPlayer = it.next(); //the first one encountered
// HockeyPlayer nextHockeyPlayer = it.next(); //the next one encountered
[Code]...
This of course produces some duplication in the print-out: These are the equal hockey players: Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
These are the equal hockey players: Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
How do I remove the duplication in the print-out?
View Replies
View Related