1. Write a method public void printInOrder( int a, int b, int c ) that takes as
ID: 3721632 • Letter: 1
Question
1. Write a method
public void printInOrder( int a, int b, int c )
that takes as its arguments three integers and prints them out in increasing order (i.e., the smallest first)
2. Write a method
public void printAgeGroup( int age )
that takes an integer age as its argument and prints one of the following messages to the console depending on the value of age:
print "infant" if age is equal to 1
print "toddler" if age is equal to 2 or 3
print "preschooler" if age is equal to 4
print "kindergartener" if age is equal to 5
print "elementary age" if age is equal to 6, 7, 8, 9 or 10
print "middle schooler" if age is equal to 11, 12 or 13
print "high schooler" if age is equal to 14, 15, 16, or 17
print "too cool" if age is equal to 18 print "old" if age is greater than 18
3. Write a method
public int findSmallest( int [ ] array )
that returns the smallest element in the provided array
Explanation / Answer
public int findSmallest( int [ ] array ) {
int min = array[0];
for(int i=1;i<array.length;i++){
if(min>array[i])
min = array[i];
}
return min;
}
public void printInOrder( int a, int b, int c ){
if(a<=b&&a<=c){
System.out.println(a);
if(b<=c){
System.out.println(b);
System.out.println(c);
} else {
System.out.println(c);
System.out.println(b);
}
} else if(b<=c){
System.out.println(b);
System.out.println(c);
System.out.println(a);
} else {
System.out.println(c);
System.out.println(b);
System.out.println(a);
}
}
public void printAgeGroup( int age ) {
if(age == 1) {
System.out.println("infant");
} else if(age>1 && age <=3) {
System.out.println("toddler");
} else if(age==4) {
System.out.println("preschooler");
} else if(age==5) {
System.out.println("kindergartener");
} else if(age>5 && age <=10) {
System.out.println("elementary age");
} else if(age>10 && age <=13) {
System.out.println("middle schooler");
} else if(age>13 && age <=17) {
System.out.println("high schooler");
} else {
System.out.println("too cool");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.