Subset - Finding Smallest Possible Sum
Aug 2, 2014
I'm trying to apply a subset problem without using O(n^2) complexity. I need to find the smallest possible sum between 2 sets of 2 numbers in a lists so n1 + n2 = n3 + n4 = sum.
I have found solutions that all involve having the sum first and just finding the two pairs which involves using a hash table and then taking each number and subtracting it off the sum and looking for possible combinations.
How to do it if nothing but the list of numbers is given. So no sum and no hint of the pairs.
I put this in the Java section because I'm doing it in Java and there seems to be no generic algorithm/programming Topic area.
View Replies
ADVERTISEMENT
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
Nov 7, 2014
I have been working on this assignment for a bit now. I seem to have most of the logic of it down, as far as I can tell, but I seem to have fallen into a bit of a brain lapse when it comes to invoking methods I've made in my main method. Here's the code:
package Module5;
import java.util.Scanner;
public class Exercise6Nine {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 numbers.");
double[] numbers = new double[10];
[Code] .....
My question is, in my main method, I have code in there to attempt to call my method "min" from below, and to use the result of the min's calculations and print them via my main. Only, I can't seem to properly invoke the method from my main method!
Also, if you could check over my min method and see if anything blatantly wrong is there and let me know, that would be great.
Looking over my question, and it seems there is no EDIT button, but I may as well put the assignment parameters here, since I asked for double checking on my method. Write a method that finds the smallest element in an array of double values using the following header:
public static double min( double[ ] array )
Write a test program that prompts the user to enter 10 numbers, invokes this method to return the minimum value, and displays the minimum value.
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 15, 2014
I've created a getMin method to return the smallest value in a queue. However, I'm having trouble calling the method in my main.
/**
* Main method.
*
* @param args
* the command line arguments; unused here
*/
public static void main(String[] args) {
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
Queue<Integer> testQueue = new Queue1L<Integer>();
[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
Oct 1, 2014
This code will let a user type 10 double's, and return the smallest of those 10 double's. But I don't really understand what happens in de last for-loop.
package hsleiden.webcat.exercise06_09;
import java.util.Scanner;
public class Exercise06_09
{
public static void main (String[] args){
double[] allNumbers = new double [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
Mar 23, 2015
import java.util.Scanner;
public class AvgLrgSml{
public static void main(String[]args){
System.out.print("Hello there. Please enter any three numbers.");
Scanner keyboard = new Scanner(System.in);
double num1 = keyboard.nextDouble();
double num2 = keyboard.nextDouble();
[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
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
May 9, 2015
Here is my code :
import java.util.Scanner;
public class smallestnumber
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int smallest =0;
int number;
[Code]...
here is the output of my code:
Enter the number
88
Enter the number
8
Enter the number
6
Enter the number
55
[Code]...
Why is it printing 0 instead of 1?
View Replies
View Related
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
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 2, 2014
I am trying to write a code that asks the user to input ten numbers and then finds and displays the smallest number out of the ones given. I am supposed to implement arrays into the program to do this. But the problem I have run into is that when I compile the code in jgrasp, I am given several error messages and I am not quite sure what I have done wrong. I'm assuming it is either a syntax or a logical error on my part but reading over the code I do not understand what is causing these errors.
This is the most current draft of my code:
import java.util.Scanner;
public class Exercise7_9 {
public static void main(String[] args) {
double[] numbers = new double[10];
//Enter ten double numbers: Scanner(System.in)
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter ten numbers: ");
[Code] ....
/* Sample Run:
Enter ten numbers: 1.9, 2.5, 3.7, 2, 1.5, 6, 3, 4, 5, 2
*/
And these are the exact error messages:
----jGRASP exec: javac -g Exercise7_9.java
Exercise7_9.java:35: error: '.class' expected
if (double m > list[i]) {
^
Exercise7_9.java:35: error: illegal start of expression
if (double m > list[i]) {
[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
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
Dec 3, 2014
I have assigned random numbers to an 2D array, and I had to write a method that prompts the user for a number between 100 and 200. If that number is in the array, it should return 1. If not, 0. It only stays 0 though, even when the number matches one of the numbers in the array.
public static int Findit(int[][] numbers, int guess) {
int Findit = numbers[0][0];
int i = 0;
int x = 0;
boolean found = false;
for(i=0;i<numbers.length;i++)
[Code] ....
View Replies
View Related
Feb 8, 2014
where to find the following import the files?
import de.uos.fmt.musitech.data.score.NotationStaff;
import de.uos.fmt.musitech.data.score.NotationSystem;
import de.uos.fmt.musitech.data.score.NotationVoice;
import de.uos.fmt.musitech.data.score.ScoreNote;
import de.uos.fmt.musitech.data.structure.Context;
import de.uos.fmt.musitech.data.structure.Note;
import de.uos.fmt.musitech.data.structure.Piece;
import de.uos.fmt.musitech.data.structure.linear.Part;
import de.uos.fmt.musitech.score.mpegsmr.Attributes;
import de.uos.fmt.musitech.utility.math.Rational;
View Replies
View Related
Nov 18, 2014
I have been struggling to find out what to change to allow my code to find the GCF of two numbers. The instance variable in the class is num, so I need to find the GCF of a and num. It works for the example problem of 25 and 15, returning 5, however it will not work for other problems.
public int findGCD(int a)
{
int result = 0;
int newNum = num;
if ((a > num) && (a % num != 0))
[Code] .....
View Replies
View Related
Oct 4, 2014
My current problem is when im trying to choose case 2 and call markItemOnLoan i cant seem to find the title i want after i enter a few. I will include all of my code but i have mentioned what I am currently stuck on :
import java.util.Scanner;
public class Library {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MediaItem mCall = new MediaItem();
Library call = new Library();// makes instance
[code]....
View Replies
View Related
Apr 1, 2014
How to declare a 2 dimensional array of interger of size 10 by 10. and then set every element of the array to 3. After this I need to add up all the element in this array and return the value.
final int ROWS = 10;
final int COLS = 10;
int[][] foo = new int[ROWS][COLS];
for (int i = 0; i < ROWS; i++){
for(int j = 0; i< COLS; i++){
foo[i][j] = 3;
}
}
This is what i have done so far(not 100% if is correct..) but how to add up all the array.
View Replies
View Related
Jan 19, 2015
I have an assessment for college, the part of my code that im struggling with is the part where the user enter their first name followed by a space and then their second name. I m using ---------------fullname = JOptionPane.showInputDialog("enter your firstname followed by a space then your second name " ); ------------- to capture this. the issue is if the user only enter 1 name I need to output an error. the issue is I cant find a way to tell if the user entered a second name. This is what I have so far:
public void makename() {
// makes an inputbox to ask their name
fullname = JOptionPane.showInputDialog("enter your firstname followed by a space then your second name " );
//separates the first and second name into 2 strings in order to make the username
[code]....
The problem is it accepts anything I type in without causing an error. What should I type in to stop this? ive tried anything but I cant find a way to tell if a surname has been entered or not.
View Replies
View Related