exercises from Java How to Program
Hi,
I am new to programming. I bought Java How to Program 6th Edition by Deitel and I begun to study. I finished the second chapter and now I'm trying to make the exercises. I did some of them but now I'm suppose to do a comparison for 5 numbers. How can I do this? I did for three numbers with if statements (if (a < b) if (b < c) a = smaller;), but for five numbers will be to many cases. To quote the problem: "Write an application that reads five integers, determines and prints the largest and the smallest integers in the group. Use only programming techniques you learned in this chapter.".
Any hint is very much appreciated :-)
Thanks
[675 byte] By [
liviudma] at [2007-10-2 5:56:23]

Have you had arrays and loops yet? That's the way to go, I think.
No, I didn't. Here is the solutions, just in case someone will need it in the future:
// ExFiveComparison.java
// compare five integers
import java.util.Scanner;
public class ExFiveComparison {
public static void main(String args[]) {
Scanner input = new Scanner( System.in );
int a; // integers
int min, max; // comparison
min = 2147483647;
max = -2147483648;
a = min;
a = max;
System.out.print("Enter the 1st integer: ");
a = input.nextInt();
if (a < min)
min = a;
if (a > max)
max = a;
System.out.print("Enter the 2nd integer: ");
a = input.nextInt();
if (a < min)
min = a;
if (a > max)
max = a;
System.out.print("Enter the 3rd integer: ");
a = input.nextInt();
if (a < min)
min = a;
if (a > max)
max = a;
System.out.print("Enter the 4th integer: ");
a = input.nextInt();
if (a < min)
min = a;
if (a > max)
max = a;
System.out.print("Enter the 5th integer: ");
a = input.nextInt();
if (a < min)
min = a;
if (a > max)
max = a;
System.out.printf("The smallest number is %d and the largest is %d",
min, max);
}
}
Solution using loops.
Usage: java Example 1 2 3 4 .... N
If you pass in a non-integer the program will throw an exception (you won't have covered this yet).public class Example
{
public static void main(String[] args)
{
int min = Integer.parseInt(args[0]);
int max = Integer.parseInt(args[0]);
int tmp;
for (int i = 1; i < args.length; i++)
{
tmp = Integer.parseInt(args[i]);
if (tmp < min)
min = tmp;
else if (tmp > max)
max = tmp;
}
System.out.println("min: " + min);
System.out.println("max: " + max);
}
}
> Any hint is very much appreciated :-)
Generally you can shift complexity between the algoritm and the data structure.
In your case you have a simple data structure and a complex algoritm. By using a more complex data structure (an array) the algoritm will become simpler.
This is a general tradeoff between the complexity of the algoritrm and the complexity of the data structures. You basically can shift the focus but the total complexity remains.
Thanks! I have another one now. It's from chapter 3. I have a class called GradeBook and another one called GradeBookTest. In class GradeBook I have a constructor:
private String courseName;
public GradeBook( String name ) {
courseName = name;
}
This is called from GradeBookTest using
GradeBook gradeBook1 - new GradeBook("some text here");
Now, I need to add another String to constructor.
private String courseName;
private String instructorName;
public GradeBook( String name, String other ) {
courseName = name;
instructorName = other;
}
Now, when I compile the project I get the following errors:
Compiling 2 source files to /home/liviudm/Java/GradeBook/build/classes
/home/liviudm/Java/GradeBook/src/GradeBookTest.java:7: cannot find symbol
symbol : constructor GradeBook(java.lang.String)
location: class GradeBook
GradeBook gradeBook1 = new GradeBook(
/home/liviudm/Java/GradeBook/src/GradeBookTest.java:9: cannot find symbol
symbol : constructor GradeBook(java.lang.String)
location: class GradeBook
GradeBook gradeBook2 = new GradeBook(
/home/liviudm/Java/GradeBook/src/GradeBookTest.java:11: cannot find symbol
symbol : constructor GradeBook(java.lang.String)
location: class GradeBook
GradeBook instructor1 = new GradeBook(
3 errors
What am I doing wrong?
> public GradeBook( String name ) {
>courseName = name;
> }[/code]
> This is called from GradeBookTest using
> GradeBook gradeBook1 - new GradeBook("some text
> here");
>
> Now, I need to add another String to constructor.
> private String courseName;
> private String instructorName;
>
> public GradeBook( String name, String other ) {
>courseName = name;
>instructorName = other;
> }
>
you have a constructor with 2 parameters (name and other) you call 1 with 1 parameter.
try this
public GradeBook(String name){
this(name, null);
}
public GradeBook(String name, String other){
courseName = name;
instructorName = other;
}
Greetz