Home > Software design >  finds the highest and lowest integers and then calculates the sum of the middle digits of those two
finds the highest and lowest integers and then calculates the sum of the middle digits of those two

Time:09-14

As the questions says... but here is a more practical example: Input: The generated numbers are: 175, 345, -567, 893, 100, -999, Output: The maximum number is 893 and the minimum is -999. The sum of the middle digits of max and mine is: 18

Basically the largest number 893 in this case, and the lowest -999 has a middle number of "9", 9 9 = 18 Im trying to understand how to pick out these two numbers (largest and smallest), but dont know how. I have done this:

public class Lab1_2 {
    public static void main (String [] args)  {
        int tal1 = (int)(Math.random() *899 100) ;
        int tal2 = (int)(Math.random() *899 100) ;
        int tal3 = (int)(Math.random() *899 100) ;
        int tal4 = (int)(Math.random() *899 100) ;
        int tal5 = (int)(Math.random() *899 100) ;
        int tal6 = (int)(Math.random() *899 100) ;

        System.out.println(tal1  " ");
        System.out.println(tal2 " ");
        System.out.println(tal3  " ");
        System.out.println(tal4 " ");
        System.out.println(tal5 " ");
        System.out.println(tal6 " ");

        if (tal1<tal2 && tal1<tal3 && tal1<tal4 && tal1<tal5 && tal1<tal6){
            System.out.println(tal1   " is the smallest");
        }
        if (tal2<tal1 && tal1<tal3 && tal2<tal4 && tal2<tal5 && tal2<tal6){

            System.out.println(tal2   " is the smallest");
        }
        if (tal3<tal2 && tal3<tal1 && tal3<tal4 && tal3<tal5 && tal3<tal6){
            System.out.println(tal3   " is the smallest");
        }
        if (tal4<tal2 && tal4<tal3 && tal4<tal1 && tal4<tal5 && tal4<tal6){
            System.out.println(tal4   " is the smallest");
        }
        if (tal5<tal2 && tal5<tal3 && tal5<tal4 && tal5<tal1 && tal5<tal6){
            System.out.println(tal5   " is the smallest");
        }
        if (tal6<tal2 && tal6<tal3 && tal6<tal4 && tal6<tal5 && tal6<tal1){
            System.out.println(tal6   " is the smallest");
        }


        if (tal1>tal2 && tal1>tal3 && tal1>tal4 && tal1>tal5 && tal1>tal6){
            System.out.println(tal1 " is the largest ");
        }
        if (tal2>tal1 && tal1>tal3 && tal2>tal4 && tal2>tal5 && tal2>tal6){
            System.out.println(tal2 " is the largest");
        }
        if (tal3>tal2 && tal3>tal1 && tal3>tal4 && tal3>tal5 && tal3>tal6){
            System.out.println(tal3 " is the largest ");
        }
        if (tal4>tal2 && tal4>tal3 && tal4>tal1 && tal4>tal5 && tal4>tal6){
            System.out.println(tal4 " is the largest");
        }
        if (tal5>tal2 && tal5>tal3 && tal5>tal4 && tal5>tal1 && tal5>tal6){
            System.out.println(tal5 " is the largest");
        }
        if (tal6>tal2 && tal6>tal3 && tal6>tal4 && tal6>tal5 && tal6>tal1){
            System.out.println(tal6 " is the largest" );

        }
    }}

I think I need to do something like this: but dont know how to implement to specifik largest and smallest.

int svar1 = (var1 % 100 / 10);
int svar2 = (var2 % 100 / 10);

CodePudding user response:

first of all , the function Math.random() as mentioned in its description :

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

so it will never return a negative values , use new Random().nextInt(2000) - 1000; instead. this will generate numbers between -1000 and 10000

but I didn't edited that part of the code , because it's not clear what is the middle digit of the number 12 as in new Random() method , any number can be generated , so I left that for you to decide , for the part of getting max and min , also you said in the comments that loops aren't allowed , so I didn't modify that much in your code.

to get the middle digit , simply (Math.abs(min) / 10) % 10 (Math.abs(max) / 10) % 10; as abs function will make sure to remove negative sign and you do (x / 10) % 10 to get the middle digit.

and here is the full code :

public class Main
{

    public static void main (String [] args)  {
        int tal1 = (int)(Math.random() *899 100) ;
        int tal2 = (int)(Math.random() *899 100) ;
        int tal3 = (int)(Math.random() *899 100) ;
        int tal4 = (int)(Math.random() *899 100) ;
        int tal5 = (int)(Math.random() *899 100) ;
        int tal6 = (int)(Math.random() *899 100) ;

        System.out.println(tal1  " ");
        System.out.println(tal2 " ");
        System.out.println(tal3  " ");
        System.out.println(tal4 " ");
        System.out.println(tal5 " ");
        System.out.println(tal6 " ");


        int min = tal1;
        int max = tal1;

        // gets the minimum
        if(min > tal2)
            min = tal2;
        if(min > tal3)
            min = tal3;
        if(min > tal4)
            min = tal3;
        if(min > tal5)
            min = tal3;
        if(min > tal6)
            min = tal3;

        //gets the max
        if(max < tal2)
            max = tal2;
        if(max < tal3)
            max = tal2;
        if(max < tal4)
            max = tal2;
        if(max < tal5)
            max = tal2;
        if(max < tal6)
            max = tal2;

        System.out.println("max = "   max);
        System.out.println("min = "   min);

        int sumOfMiddleDigits = (Math.abs(min) / 10) % 10    (Math.abs(max) / 10) % 10;
        System.out.println("sum of middle digits = "   sumOfMiddleDigits);
    }

}

and this is the output :

990 
676 
751 
653 
160 
307 
max = 990
min = 751
sum of middle digits = 14

CodePudding user response:

I'll assume there will always be a fixed number of generated numbers, and there will always be few. If the number of numbers is variable, or too many to have a separate variable for ech, perhaps that should be addressed in another question.

The common way to find a maximum value of elements of an array is similar to this:

int[] arr; 
...
// code to new the array and put values into it
...
int max = -9999999;
for (int i = 0; i < arr.length;   i) {
   if (arr[i] > max) max = arr [i];
}

The value -9999999 is a value that is lower than any possible value in arr, and depends on the application. But, if a possible value in arr could actually be the lowest value allowed for the data type, you could use this, assuming arr has at least one element:

int max = arr[0];
for (int i = 1; i < arr.length;   i) {
   if (arr[i] > max) max = arr[i];
}
 

But, what if your professor forbade the use of arrays and loops?

The general form of an if statement is if (b) c where b is a Boolean expression, and c is an imperative statement. An imperative statement is one that tells the program to do something -- take an action. It can be an assignment, a method call, exit, jump (such as a GOTO in old languages), and so on. It can be another if statement.

if (santaIsReal) System.out.println ("Ho, ho, ho!"); 

What if you wanted the program to do more than one thing? Well, Java allows you to put a block of code for c. You group the statements by putting them between { and }:

if (today == christmas) { 
   santa.chimneySlide (myHouse);
   santa.leaveGift (bobby);
   santa.leaveGift (sue);
   santa.eatCookies ();
}

So, where you have code like this:

if (tal3<tal2 && tal3<tal1 && tal3<tal4 && tal3<tal5 && tal3<tal6){
        System.out.println(tal3   " is the smallest");
}

You could add a line:

 if (tal3<tal2 && tal3<tal1 && tal3<tal4 && tal3<tal5 && tal3<tal6){
        theMin = tal3;
        System.out.println(tal3   " is the smallest");
}

You would do something similar to find the maximum in the appropriate section of code.

Comment: You can remove the lines where you display the value of one of the tal variables with "is the smallest" or "is the largest." Once you find the largest and smallest, display the values using your the variables for largest and smallest.

You could reduce the number of lines in your code by using methods in the Math API: int min (int, int) and int max (int, int).

Suppose I want to find the maximum of 4 variables, a, b, c, and d using Math.max:

int theMax = Math.max (a, Math.max (b, Math.max (c, d)));

I infer you want to exclude single digit and double digit numbers from your randomly generated numbers. A poor way, IMO, to do this is to try again when the result is in the "rejected" range. That would use a loop:

int myRand; 
do { 
     myRand = (int)(Math.random() * 2000 - 999 );
} while (myRand > 999 || (myRand >= -99 && myRand <= 99)); 

A better way is to generate two random values for each number:

int myRand = (int)(Math.random() * 1000   100);
if (Math.random()< 0.50) myRand = - myRand;

The code you have will get the middle digit of a 3 digit positive number:

int svar1 = (var1 % 100 / 10);

But, to be on the safe side, use Math.abs (int) to get it to work for both a positive value and a negative value:

int svar1 = (Math.abs(var1) % 100 / 10);
  • Related