0% found this document useful (0 votes)
49 views9 pages

M105 Exercise 03 Solution

The document contains solutions to 13 exercises related to Java programming. The exercises cover topics like mathematical functions, conditional statements, strings, user input, and finding maximum values. Detailed code solutions are provided for each exercise written in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views9 pages

M105 Exercise 03 Solution

The document contains solutions to 13 exercises related to Java programming. The exercises cover topics like mathematical functions, conditional statements, strings, user input, and finding maximum values. Detailed code solutions are provided for each exercise written in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

M105 – Meeting 3 - Extra Exercises

Exercise 1:
Given the following declarations: long i; double d;
What are the values of and after executing each of the following Java
statements?

a) d = Math.abs(-3.5);
b) d = Math.ceil(3.5);
c) d = Math.ceil(-3.5);
d) d = Math.floor(3.5);
e) d = Math.floor(-3.5);
f) i = Math.round(3.5);
g) i = Math.round(3.45);
h) i = Math.round(-3.5);
i) d = Math.max(-2.5, -3.5);
j) d = Math.pow(3, 2);
k) d = Math.sqrt(4);

Solution:

a) 3.5
b) 4.0
c) -3.0
d) 3.0
e) -4.0
f) 4
g) 3
h) -3
i) -2.5
j) 9.0
k) 2.0

Exercise 2: (from TMA Fall 2012-2013)


Write a Java program that prompts the user to enter 2 positive integers
(say x and y)and prints on the screen the result of the following
mathematical function: (Try to write the result to the standard output
without using any additional variables except x and y)

Hints:
Use the methods of the class Math.
|x| = the absolute value of x
= the ceiling (ceil) value of x

1
Solution:

import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
int x,y;
Scanner input=new Scanner(System.in);
System.out.print("Enter two Integer values: ");
x=input.nextInt();
y=input.nextInt();
System.out.print("The output is"+(
Math.ceil((Math.sqrt(Math.abs(Math.pow(x, 5)-Math.pow(y, 5)))+x*y)/(x+y))+2));
} }

Exercise 3:
What is the exact output of the following piece of code?
int m = 4;
System.out.println("m = " + m++);
if (m != 5)
m*=2;
else
m/=2;
System.out.println("m = " + m);
int n = m-- == 2 ? 50 : 100;
System.out.printf("m = %d and n = %d\n", m, ++n);

Solution:
m=4
m=2
m = 1 and n = 51

Exercise 4:
Write a Java program that prompts the user to enter an integer and
displays on the screen a message if this integer is positive or not.
Solution:
import java.util.Scanner;
public class Q4 {
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Enter an integer: ");
int x= input.nextInt();
if(x>=0)
System.out.print("x is positive");
else
System.out.print("x is negative");
} }
2
Exercise 5:
Write a Java program that prompts the user to enter an integer and
displays on the screen a message if this integer is odd or even. (Try
negative integers)

Hint:
Use the % operator with the integer 2.

Solution:

import java.util.Scanner;
public class Q5 {
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Enter an integer: ");
int x= input.nextInt();
if(x%2==0)
System.out.print("x is Even");
else
System.out.print("x is Odd");
}
}

Exercise 6: (from exercise 2.26 “Java How to program” 7/e)


Write a Java program that prompts the user to enter 2 positive integers
and displays on the screen a message if the first integer is a multiple
of the second one or not.

Hint:
Use the % operator.

Solution:

import java.util.Scanner;
public class Q6 {
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Enter two integers: ");
int x=input.nextInt();
int y=input.nextInt();
if(x%y==0)
System.out.println("the first integer is a multiple of the second one");
else
System.out.println("the first integer is not a multiple of the second one");
}
}

3
Exercise 7:
Write a Java program that prompts the user to enter a word and displays on the screen a message
if this word starts with the letter 'A' or not.
Hint:
Use the String method charAt()

Solution:
import java.util.Scanner;
public class Q7 {
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Enter a word: ");
String word=input.next();
if(word.charAt(0)== 'A')
System.out.println("The word starts with A");
else
System.out.println("The word does not start with A ");
}
}

Exercise 8:
Write a Java program that prompts the user to enter 2 words and displays on the screen a
message if the 2 words are identical or not.
Hint:
Use the String method equals()

Solution:
import java.util.Scanner;
public class Q8 {
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Enter two word: ");
String word1=input.next();
String word2=input.next();

if(word1.equals(word2))
System.out.println("The two words are identical");
else
System.out.println("T he two words are not identical");
}
}

4
Exercise 9: (from TMA Spring 2012-2013)
The following code includes 10 errors (an error per line).
a) Discover and classify them as compilation errors, fatal logical errors
or nonfatal logical errors.
b) Correct them and give the exact output after correcting the code.

int x, y, my Num = 0;
double d = 10 + x; x = 3;
y = d + 2;
if (! x>5)
System.out.println('Result1 = ' + x);
Else
System.out.printIn("Result1 = " + y);
System.out.println("Result2 = %d" + myNum);
System.out.println("Result3 = " + x / myNum); myNum++;
System.out.printf("Result4 = %.0f\n" + d);

Solution:

int x, y, my Num = 0; Compile Error: myNum no space


Correction: int x, y, myNum = 0;
double d = 10 + x; x = 3; Compile Error: x must initialize first
Correction: x = 3;double d = 10 + x;
y = d + 2; Compile Error: can not store double into int
Correction: y = x + 2;
if (! x>5) Compile Error: Compile Error : () for
expression
Correction: if (! (x>5))
System.out.println('Result1 = ' + Compile Error: "" not ''
x); Correction: System.out.println("Result1 = " +
x);
Else Compile Error: e in else must be small letter
Correction: else
System.out.printIn("Result1 = " + Compile Error: println not printIn
y); Correction: System.out.println("Result1 = " +
y);
System.out.println("Result2 = %d" nonfatal logical errors
+ myNum); Correction: System.out.println("Result2 = " +
myNum);
System.out.println("Result3 = " + fatal logical errors Divide by 0
x / myNum); myNum++; Correction: System.out.println("Result3 = " +
x / ++myNum);
System.out.printf("Result4 = fatal logical errors , not +
%.0f\n" + d); Correction: System.out.printf("Result4 =
%.0f\n" , d);

Output:

Result1 = 3
Result2 = 0
Result3 = 3
Result4 = 13

5
Exercise 10:
Write a Java program that prompts the user to enter an integer and
displays on the screen a message if this integer is positive, negative or
zero.

Solution:

import java.util.Scanner;
public class Q1 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter an integer: ");
int x= input.nextInt();
if(x>0)
System.out.print("x is positive");
else if (x<0)
System.out.print("x is negative");
else
System.out.print("x is 0");
}
}

Exercise 11:
What is the exact output of the following piece of code?
int w = 7, z = 10;
if (w <= 7 && z != 10) System.out.println("ONE");
if (w <= 7 || z != 10) System.out.println("TWO");
if (w <= 7 ^ z == 10) System.out.println("THREE");
if (!(w <= 7 && z != 10)) System.out.println("FOUR");
if (w == 8); System.out.println("FIVE");
if (z == 10)
System.out.println("YES");
else;
System.out.println("OK");
if (w != 7)
System.out.println("WWW");
if (z % 5 == 0)
System.out.println("ZZZ");
else
System.out.println("AAA");

Solution:
TWO
FOUR
FIVE
YES
OK
ZZZ
6
Exercise 12:
Write a Java program that prompts the user to enter an integer and displays on the screen a
message if this integer is divisible by both 2 and 3 or not.
Hint: Use the % operator.

Solution:
import java.util.Scanner;
public class Q12 {
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Enter an integer: ");
int x= input.nextInt();
if(x%2==0 && x%3==0)
System.out.println("x is divisible by both 2 and 3 ");
else
System.out.print("x is not divisible by both 2 and 3 ");
}
}

Exercise 13:
Write a Java program that prompts the user to enter 3 integers and print on the screen the largest
of them.

Solution:
import java.util.Scanner;
public class Q13 {
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Enter three integer: ");
int x= input.nextInt();
int y= input.nextInt();
int z= input.nextInt();

int max= x;
if(y>max)
max=y;
if(z>max)
max=z;
System.out.printf("The largest number between %d, %d and %d is : %d\n ",x,y,z,max);
}
}

7
Exercise 14: (from TMA Fall 2013-2014)
What is the exact output of the following piece of code?

int x = 9, y = 4, z;
switch (x + 1 % y) {
case 1: System.out.println("1: " + x + y);
case 2: System.out.println("2: " + x + y);
case 10: System.out.println("10: " + x + y);
default: System.out.printf("def: %d\n", x + y);
}
z = 7;
char c = (z == 6) ? 'Y' : 'N';
System.out.printf("c = %c\n", c);
String text = "TEXT";
if ( ! text.equals("text") && (x == 9.0) )
System.out.println(text);
if (y > x);
System.out.printf("%.0f\nOK\n", 99.7);

Solution:
10: 94
def: 13
c = N
TEXT
100
OK

8
Exercise 15: (from TMA Fall 2013-2014)
Academic status of AOU students could be:
- "regular" if their GPA ≥ 2
- "warned" if their GPA < 2 and number of warnings they received < 4
- "dismissed" if their GPA < 2 and number of warnings they received = 4
Write a Java application that reads from user the GPA of a student as a
real number and number of warnings he/she received, then prints the
academic status of the student.

Solution:

import java.util.Scanner;
public class Q15 {
public static void main(String[] args) {
double GPA;
int warnings;
Scanner input = new Scanner(System.in);
System.out.print("Insert GPA of a student: ");
GPA = input.nextDouble();
if (GPA >= 2)
System.out.println("The student is regular.");
else
{
System.out.print("Insert number of warnings: ");
warnings = input.nextInt();
if (warnings < 4)
System.out.println("The student is warned.");
else
System.out.println("The student is dismissed.");
} //end the outer if-else
} //end main method
} //end class

You might also like