Largest and Smallest Integer - Exercise from Deitel'S Java Book

Largest and smallest integer - exercise from Deitel's Java book

In the second block you check with smallest and assign to largest.

Separating the Digits in an Integer - exercise from Deitel's Java book

You know that the number is 5 digit long.

What about

number / 10 000 to retrieve the first digit.
number = reminder
number / 1000 to retrieve the second digit.
number = reminder
number / 100 to retrieve the third digit.
number = reminder
number / 10 to retrieve the fourth digit.
and reminder is the 5th one.

Comparing multiple integers in IF statement, Java

The expression:

firstInt > secondInt, thirdInt, fourthInt, fifthInt

is not valid because Java is a computer language rather than a natural language (like English).

If you want to do it that way, what you need is the and conjunction, with multiple complete conditions (using a,b,c,d,e to minimise the code):

if ((a >= b) && (a >= c) && (a >= d) && (a >= e)) { // a >= b,c,d,e
System.out.println ("Largest is: " + a);
} else if ((b >= c) && (b >= d) && (b >= e)) { // b >= c,d,e
System.out.println ("Largest is: " + b);
} else if ((c >= d) && (c >= e)) { // c >= d,e
System.out.println ("Largest is: " + c);
} else if (d >= e) { // d >= e
System.out.println ("Largest is: " + d);
} else { // e > d
System.out.println ("Largest is: " + e);
}

keeping in mind that, once you decide a (or any of the others in later steps) isn't the largest, you never need to check it again, as one of the others will be larger than it.


Or you can use the facilities of the Java standard Math package, where Math.max(a,b) will give you the largest of the values from a and b:

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

Here, you define the largest of all five numbers as the maximum of the pair {a, maximum-of-bcde} then use a similar method to work out maximum-of-bcde as the maximum of the set {b, maximum-of-cde} and so on.

Or, in other words, choose the largest from {d,e} and call it x. Then choose the largest from {c,x} and call it y. Then the largest from {b,y} and call it z. Finally the largest from {a,z} and that's your largest value of the five.

Though, to be honest, if you're on chapter one of an introductory text, it probably hasn't covered much in the Java standard libraries yet.


But, if you're after a method where it's perhaps easier for a beginner to understand the flow, I don't think you can go past this one:

int maxOfFive (int a, int b, int c, int d, int e) {
int largest = a;
if (b > largest) largest = b;
if (c > largest) largest = c;
if (d > largest) largest = d;
if (e > largest) largest = e;
return largest;
}

This is basic selection of the largest value as a person would do it, scanning the list one by one and just remembering which value was the highest, then returning that.

Or, if you haven't learned about functions yet (and reverting to your original variables), just implement the same code without a function call:

int largest = firstInt;
if (secondInt > largest) largest = secondInt;
if (thirdInt > largest) largest = thirdInt;
if (fourthInt > largest) largest = fourthInt;
if (fifthInt > largest) largest = fifthInt;
System.out.println ("Largest is: " + largest);

I believe that's probably only using stuff you've already indicated that you know about (variables, single-condition if statement, and output).

C++ Exercises for Beginner to Intermediate Programmers

I will give you the suggestions of areas you have to be strong if you want to be a c++ developer.

The areas you have to improve is

Garbage Collection - Freeing the memory is done by us.So we have to take care of this part.

Multithreading - Creating multi threaded program and try to use mutex,events,critical section everything into your program.So that you can understand the importance of OS concepts in C.

STL - Learn Templates and STL concepts.

MFC - Explore MFC Classes and try to use it in your program.

Learn All these stuffs and then pick some desktop applications like MSPaint and try to create the application like that and create all that MSPaint functionalities into your application.

Once You finish MSPaint application and try to implement additional features like photoshop functionalities.

These Project will give you a lot of experience in C++.

java: Precision in double numbers

This works pretty good, remember that the author hasn't teach how to compare strings or anything like that yet, the code works ok now.

import javax.swing.JOptionPane;

public class Pi {
public static void main (String args[]){
double pi = 4;
double fraccion = 3.0;
double cociente ;
int firstSixDigits;
for ( int i = 1 ; i <= 200000 ; i++ ){

cociente = 4.0/fraccion;

if ( i % 2 == 0 )
pi += cociente;
else
pi -= cociente;

System.out.printf("El valor de pi en el término %d, es : %.10f \n", i , pi);

firstSixDigits = (int) ( pi * 100000.0 ) ;



if ( firstSixDigits % 314159 == 0 )
JOptionPane.showMessageDialog(null, String.format("Pi vale: %.10f este es el primer valor que empieza con 3.14159 en el término %d", pi, i));

fraccion += 2.0;

}

System.exit(0);
}
}

... also, and to close this ...

Actually, the only thing to be corrected was the arguments I used (pi - 3.141595 < 0.000005 ) does the trick), using Math.abs was a great idea... because when, for example pi = 3.141593...

then pi - 3.141595 is equal to -0.000002 (not always, of course, let's asume the number ends right there, and there are no more decimals) ; Math.abs (-0.000002) = 0.000002 is < 0.000005 ... and for any value from 3.14159x with x = { 0 .. 9 } the value 'Math.abs (pi - 3.141595 ) < 0.000005' is true, so it always recognizes when pi starts with 3.14159.



Related Topics



Leave a reply



Submit