Java debug help needed: display largest of 3 numbers - java

This is a program that is supposed to prompt the user to enter three numbers and
then display the largest of these numbers. However, there are logic errors in it. I'm stuck on trying to figure out where this little bugger is. Please use your expertise to lend me a hand. I am a student, so please don't rage on me \:p
import java.util.*;
public class HA8LargestErr {
private int num1;
private int num2;
private int num3;
public HA8LargestErr() {
num1 = 0;
num2 = 0;
num3 = 0;
}
public void getNumsFromUser() {
Scanner input = new Scanner (System.in);
System.out.println("Enter three numbers: ");
num1 = input.nextInt();
num2 = input.nextInt();
num3 = input.nextInt();
}
public int returnLargest() {
if (num1 > num2 && num1 > num3)
return num1;
if (num2 > num3 && num2 > num1)
return num2;
return num3;
}
public static void main(String[] args) {
HA8LargestErr data = new HA8LargestErr();
data.getNumsFromUser();
System.out.println ("The largest is : " + data.returnLargest());
}
}

Replace your implementation of returnLargest with
public int returnLargest() {
if (num1 >= num2 && num1 >= num3)
return num1;
if (num2 >= num3)
return num2;
return num3;
}
Or use Math.max as suggested above.
Edit:
You need to use >= instead of > because otherwise num3 will be returned when num1 and num2 are equal and larger than num3.

Related

Double not being passed down in from method to method

I designed a small program where I have three exam grades and I use the Grades class to compute the average of the three. Also, I prompt for the exam number (1,2, or 3) and it should return it. However, I keep getting 0.0 as the output for both the average exam score and chosen exam score.
package GradesClass;
import java.util.Scanner;
public class GradesDriver {
public static void main(String[] args) {
Grades school = new Grades(90.9,87.9,99.9);
Scanner in = new Scanner(System.in);
System.out.println("Enter desired test number: ");
int testnumber = in.nextInt();
System.out.println(school);
System.out.println("Exam score: " + school.getGrades(testnumber));
}
}
package GradesClass;
public class Grades {
private double num1, num2, num3;
private int testnumber;
private double average;
public Grades(double num1, double num2, double num3) {
num1 = 0;
num2 = 0;
num3 = 0;
}
public void setGrades(double scorenumber, int testnumber) {
if (testnumber == 1) {
num1 = scorenumber;
} else if (testnumber == 2) {
num2 = scorenumber;
} else {
num3 = scorenumber;
}
}
public double getGrades(int testnumber) {
if (testnumber == 1) {
return(num1);
} else if (testnumber == 2) {
return(num2);
} else {
return(num3);
}
}
public double average(double num1, double num2, double num3) {
average = ((num1+num2+num3)/3.0);
return(average);
}
public String toString() {
return("Average: " + average);
}
}
In your constructor for Grades you are setting the member variables to zero instead of the values supplied in the parameters. Change the constructor to
public Grades(double num1, double num2, double num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}

Can't sort 3 floating point numbers in Java

I am wring program that sorts three integers. But I am not getting result for the input {1,3,2}. Probably some logic mistake in the 4th if statement.
The numbers are taken as input.
// program to sorting 3 double.
import java.util.*;
public class Sorting {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Input the numbers for sorting");
double num1 = in.nextDouble();
double num2 = in.nextDouble();
double num3 = in.nextDouble();
double a = 0;
double b = 0;
double c = 0;
if ((num1 > num2) && (num2 > num3)) {
a = num1;
b = num2;
c = num3;
}
if ((num1 > num2) && (num3 > num2)) {
a = num1;
b = num3;
c = num2;
}
if ((num2 > num1) && (num1 > num3)) {
a = num2;
b = num1;
c = num3;
}
if ((num2 > num1) && (num3 > num1)) {
a = num2;
b = num3;
c = num1;
}
if ((num3 > num1) && (num1 > num2)) {
a = num3;
b = num1;
c = num2;
}
if ((num3 > num1) && (num2 > num1)) {
a = num3;
b = num2;
c = num1;
}
System.out.println(" The numbers are in" + c + "< " + b + "< " + a);
}
}
If you are happy with an array, you may use the Arrays.sort() method:
double[] numbers = new double[] { num1, num2, num3 };
Arrays.sort(numbers);
System.out.println("The numbers are " + Arrays.toString(numbers));
Given input 1 3 2 this prints The numbers are [1.0, 2.0, 3.0].
I would do it with arrays. Example :
class DoubleSorter {
public double[] sorted;
public boolean[] deleted;
public int array_position=0;
public int element_to_delete=0;
public DoubleSorter() {
}
public void self_calling_sorter(double[] numbers) {
double bestnum=0;
boolean finished=true;
for (int i=0; i < numbers.length; i++) {
if (deleted[i] == false) {
if (finished==true) { //First set to bestnum
bestnum=numbers[i];
element_to_delete=i;
finished=false;
}
else {
if (numbers[i] >= bestnum) {
bestnum=numbers[i];
element_to_delete=i;
}
}
}
}
deleted[element_to_delete]=true;
if (finished==false) {
sorted[array_position]=bestnum;
array_position++;
self_calling_sorter(numbers);
}
}
public double[] sort(double[] numbers) {
sorted=new double[numbers.length];
deleted=new boolean[numbers.length];
for (int i=0; i < numbers.length; i++) {
deleted[i]=false;
}
self_calling_sorter(numbers);
return sorted;
}
}
This function takes an array, looks for the biggest element, writes it down, sets it to null, calls itself, looks for the biggest element, excludes the taken, because they were set to null, writes the biggest number again...
Example of how it works :
numbers={1,3,2};
result={null,null,null};
First sorting :
result={3,null,null};
numbers={1,null,2};
Second sorting :
result={3,2,null};
numbers={1,null,null};
Third sorting :
result={3,2,1};
numbers={null,null,null};
A fourt sorting isnt executed because it notices that bestnum is null because it couldnt be set.
This function is more flexible and can sort a almost infinite amount of numbers much easier.
And here is how I would integrate it in your code :
import java.util.*;
public class Sorting {
public static void main(String[] args) {
DoubleSorter sorter=new DoubleSorter();
Scanner in = new Scanner(System.in);
System.out.println("How many numbers would you like to sort ?");
int count = in.nextInt();
double[] numbers=new double[count];
for (int i=0; i < count; i++) {
System.out.println("Number "+Integer.toString(i));
numbers[i]=in.nextDouble();
}
System.out.println("The sorted list is : "+Arrays.toString(sorter.sort(numbers)));
}
}
EDIT :
I saw this and realized that you'd probably like to sort them the other way round.
System.out.println(" The numbers are in" + c + "< " + b + "< " + a);
Then you'll only have to change this :
if (numbers[i] >= bestnum) {
bestnum=numbers[i];
element_to_delete=i;
}
to this :
if (numbers[i] <= bestnum) {
bestnum=numbers[i];
element_to_delete=i;
}

Finding the smallest, largest, and middle values in Java

I did with this code. Is it correct way? I want to sort the numbers in ascending order. Is there better way for this?
import java.lang.Math;
public class Numbers
{
public static void main(String[] args)
{
int a=1;
int b=2;
int c=3;
if (a<b && a<c)
System.out.println("Smallest: a");
else if (a>b && a>c)
System.out.println("Biggest: a");
else if (a>b && a<c)
System.out.println("Mid: a");
else if (a<b && a>c)
System.out.println("Mid: a");
if (b<c && b<a)
System.out.println("Smallest: b");
else if (b>c && b>a)
System.out.println("Biggest: b");
else if (b>c && b<a)
System.out.println("Mid: b");
else if (b<c && b>a)
System.out.println("Mid: b");
if (c<a && c<b)
System.out.println("Smallest: c");
else if (c>a && c>b)
System.out.println("Biggest: c");
else if (c>a && c<b)
System.out.println("Mid: c");
else if (c<a && c>b)
System.out.println("Mid: c");
}
}
Expanding on Steve's answer (I assume you are new to Java and need a more complete example):
import java.util.Arrays;
public class Numbers
{
public static void main(String[] args)
{
int a=3;
int b=2;
int c=1;
int[] numbers = {a,b,c};
Arrays.sort(numbers);
System.out.println("The highest number is "+numbers[2]);
System.out.println("The middle number is "+numbers[1]);
System.out.println("The lowest number is "+numbers[0]);
}
}
You can store the three numbers in an array and then do
Arrays.sort(numbers);
/* numbers[0] will contain your minimum
* numbers[1] will contain the middle value
* numbers[2] will contain your maximum
*/
That's all!
In general it would be best to use a loop and a array for this type of thing that way if you have more than 3 numbers it will still work. Also you wont have to type nearly as much. Try something like this for finding the smallest number.
MyArray = new int[3];
MyArray[0] = 1;
MyArray[1] = 2;
MyArray[2] = 3;
int temp = a;
for (int i = 0; i < (number of numbers to check in this case 3); i++){
if (MyArray[i] < temp){
temp = MyArray[i];
}
}
System.out.println("Smallest number is: " + temp);
import java.util.Scanner;
public class SortingIntegers {
public static void main (String[] args){
int num1;
int num2;
int num3;
int largerstNum;
int smallestNum;
int middleNum;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the First Integer");
num1 = sc.nextInt();
System.out.println("Pleas enter the Second Integer");
num2 = sc.nextInt();
System.out.println("Please enter the third Integer");
num3 = sc.nextInt();
if (num1 > num2){
if (num1 > num3){
largerstNum = num1;
if (num2 > num3){
middleNum = num2;
smallestNum = num3;
}else {
middleNum = num3;
smallestNum = num2;
}
}
}else {
if (num1 > num3){
middleNum = num1;
if (num2 > num3){
largerstNum = num2;
smallestNum = num3;
}else {
largerstNum = num3;
smallestNum = num2;
}
}else {
smallestNum =num1;
if (num2 > num3){
largerstNum = num2;
middleNum = num3;
}else {
largerstNum = num3;
middleNum = num2;
}
}
System.out.println("Highest Number is : " + largerstNum);
System.out.println("Smallest Number is : " + smallestNum);
System.out.println("Middle Number is : " + middleNum);
}
}
}

How to print out the GCD in different format

I am trying to ask the user to enter two integers and have the message to say
"The GCD of "first integer" and "second integer" is "GCD"
I have all my calculations right but it is just printing out my num1 for all values.
import java.util.Scanner;
public class Assignment3
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int num1 = scan.nextInt();
System.out.print("Enter the second integer: ");
int num2 = scan.nextInt();
while (num1 != num2)
{
if (num1> num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
}
System.out.println("The gcd of" + num1 + " and " + num2 + " is " + num1);
}
}
In your while loop you check if num1 != num2 so when the println gets executed num1 will have the same value as num2.

How does number swapping work with if statements?

I want to know exactly how these if statements are switching the numbers. I have never been asked to do non-descending order before so I took a little snip off the internet.
import java.util.Scanner;
/**
* Created by Nicholas on 10/26/2015.
*/
public class Main {
final static Scanner userInput = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please enter three numbers.");
System.out.println();
System.out.print("Number 1: ");
int num1 = userInput.nextInt();
System.out.println();
System.out.print("Number 2: ");
int num2 = userInput.nextInt();
System.out.println();
System.out.print("Number 3: ");
int num3 = userInput.nextInt();
System.out.println();
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
if (num2 > num3) {
int temp = num2;
num2 = num3;
num3 = temp;
}
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
System.out.print("The numbers in non-descending order are, " + num1 + " " + num2 + " " + num3);
}
}
The if statements switch the numbers by creating a temporary value to store the old number in before setting it to the new number, the other number is then set to the old, saved number
A more interesting way of switching numbers is with bitwise operators
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
This works from xoring the bits together, and because xor inverts itself when given the same value ((A xor B) xor B) == A

Categories