Double not being passed down in from method to method - java

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;
}

Related

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);
}
}
}

"Cannot find symbol error" in my Triangle class

I can't figure out why I'm getting the cannot find symbol errors for the num and triangle type variables.
This is the class:
public class Triangle
{
private int s1;
private int s2;
private int s3;
public Triangle (int s1, int s2, int s3)
{
s1= num1;
s2= num2;
s3= num3;
}
public String toString()
{
return (s1 + " " + s2 + " " + s3);
}
public boolean is_equilateral(){
if(s1==s2 && s2==s3 && s3==s1)
{
return Triangle.is_equilateral;
}
else
{
return false;
}
}
public boolean is_isosceles(){
if((s1==s2)||(s2==s3)||(s3==s1))
{
return Triangle.is_isosceles;
}
else
{
return false;
}
}
public boolean is_scalene(){
if(s1!=s2 && s2!=s3 && s3!=s1)
{
return Triangle.is_scalene;
}
else
{
return false;
}
}
}
and this is the program:
import java.util.Scanner;
public class Assignment5 {
//===========================================================
// Create and determine properties of various triangles.
//===========================================================
public static void main (String[] args) {
Scanner console = new Scanner(System.in);
int num1;
int num2;
int num3;
String another;
do
{
System.out.println("Enter the sides of the triangle: ");
num1 = console.nextInt();
num2 = console.nextInt();
num3 = console.nextInt();
Triangle myTriangle = new Triangle (num1, num2, num3);
System.out.println(myTriangle.toString() + " triangle:");
//check the isosceles
if (myTriangle.is_isosceles())
System.out.println("\tIt is isosceles");
else
System.out.println("\tIt is not isosceles");
//check the equilateral
if (myTriangle.is_equilateral())
System.out.println("\tIt is equilateral");
else
System.out.println("\tIt is not a equilateral");
//check the scalene
if (myTriangle.is_scalene())
System.out.println("\tIt is scalene");
else
System.out.println("\tIt is not scalene");
System.out.println();
System.out.print("Check another Triangle (y/n)? ");
another = console.next();
} while (another.equalsIgnoreCase("y"));
} // method main
} // class Assignment5
I'm fairly new to Java so sorry if this is an obvious problem.
private int s1; // these belong to the class
private int s2;
private int s3;
public Triangle (int s1, int s2, int s3) // these belong to the constructor
{
s1= num1; // num1 isn't declared anywhere
s2= num2;
s3= num3;
}
Both your class variables and the arguments to your constructor are named s1 - s3.
You need to either change the arguments to num1 - num3, or change the assignment statements to use the this keyword to refer to the class variables. Here's the first method:
private int s1; // these belong to the class
private int s2;
private int s3;
public Triangle (int num1, int num2, int num3) // these belong to the constructor
{
s1 = num1;
s2 = num2;
s3 = num3;
}
Providing Constructors for Your Classes

Basic Calculator

Im having problems with my program. the program displays the first JOption message dialog box, but when you input a value, it fails to display the second dialog box??
import java.util.Scanner;
import javax.swing.JOptionPane;
public class javaCalculator
{
public static void main(String[] args)
{
int num1;
int num2;
String operation;
Scanner input = new Scanner(System.in);
JOptionPane.showInputDialog(null,"please enter the first number");
num1 = input.nextInt();
JOptionPane.showInputDialog(null,"please enter the second number");
num2 = input.nextInt();
JOptionPane.showInputDialog(null,"Please enter operation");
operation = input.next();
if (operation.equals ("+"))
{
JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 + num2));
}
if (operation.equals ("-"))
{
JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 - num2));
}
if (operation.equals ("/"))
{
JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 / num2));
}
if (operation.equals ("*"))
{
JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 * num2));
}
}
}
Read the numbers from the dialog output instead of from System.in:
String firstNumber = JOptionPane.showInputDialog(null,"please enter the first number");
if (firstNumber != null) {
num1 = Integer.parseInt(firstNumber);
}
Scanner is unnecessary in your code.
You can try using that sample code:
import javax.swing.JOptionPane;
public class Hw4_3 {
private static boolean flag;
public static void main(String[] args) {
do {
String[] expression = getTask();
double result = calculate(expression);
display(expression, result);
repeat();
} while (flag);
}
public static String[] getTask()
{
String bleh = JOptionPane.showInputDialog(null, "Please enter what you would like to calculate: ");
String[] tokens = bleh.split(" ");
return tokens;
}
public static double calculate(String[] data)
{
double num1 = Double.parseDouble(data[0]);
double num2 = Double.parseDouble(data[2]);
double result = 0;
if(data[1].equals("+"))
{
result = add(num1, num2);
return result;
}
else if(data[1].equals("-"))
{
result = subtract(num1, num2);
return result;
}
else if(data[1].equals("*"))
{
result = multiply(num1, num2);
return result;
}
else
{
if(num2 == 0)
{
JOptionPane.showMessageDialog(null, "Sytax error, divide by zero");
}
result = divide(num1, num2);
return result;
}
}
public static double add(double num1, double num2)
{
return num1 + num2;
}
public static double subtract(double num1, double num2)
{
return num1 - num2;
}
public static double multiply(double num1, double num2)
{
return num1 * num2;
}
public static double divide(double num1, double num2)
{
return num1 / num2;
}
public static void display(String[] data, double result)
{
if(data[1].equals("/") && data[2].equals("0"))
{
}
else
{
JOptionPane.showMessageDialog(null, data[0] + " " + data[1] + " " + data[2] + " = " + result);
}
}
public static void repeat()
{
int bleh = JOptionPane.showConfirmDialog(null, "More?",null, JOptionPane.YES_NO_OPTION);
if(bleh == JOptionPane.NO_OPTION)
{
flag = false;
JOptionPane.showMessageDialog(null, "Have a good day.");
}
else
flag = true;
}
}

Java debug help needed: display largest of 3 numbers

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.

Categories