Object Oriented Assignment 2
Object Oriented Assignment 2
Name ID
Submitted by Gosaye Emshaw UGR/1636/12
Submitted to Mr. Samuel Melke
Submission date 10/8/2021
1
Exercise 2
2
2.12 Given that y = ax3 + 7, which of the following are correct Java statements for this
equation?
a) y = a * x * x * x + 7;
b) y = a * x * x * (x + 7);
c) y = (a * x) * x * (x + 7);
d) y = (a * x) * x * x + 7;
e) y = a * (x * x * x) + 7;
f) y = a * x * (x * x + 7);
answer: a, d, e.
2.13 State the order of evaluation of the operators in each of the following Java statements, and
show the value of x after each statement is performed:
a) x = 7 + 3 * 6 / 2 - 1;
Ans: *, /, -, + and x=15
b) x = 2 % 2 + 2 * 2 - 2 / 2;
Ans: %, *, /, +, - and x=3
c) x = (3 * 9 * (3 + (9 * 3 / (3))));
Ans: (27 * (3 + 9)) = 27 * 12=324
The value of x is 324
2.14 Write an application that displays the numbers 1 to 4 on the same line, with each pair of
adjacent numbers separated by one space. Use the following techniques:
a) Use one System.out.println statement.
public class number1_4 {
public static void main (String [ ] args)
{System.out.println("1 2 3 4");
}
b) Use four System.out.print statements.
public class number1_4 {
public static void main (String [ ] args)
{System.out.print("1 ");
System.out.print("2 ");
System.out.print("3 ");
System.out.print("4");
}
}
c) Use one System.out.printf statement.
public class number1_4 {
public static void main (String [ ] args)
{
System.out.printf("%d %d %d %d ",1,2,3,4);
3
}
2.15 (Arithmetic) Write an application that asks the user to enter two integers, obtains them
from the user and prints their sum, product, difference and quotient (division).
import java.util.Scanner;
public class Arthimetic
{
public static void main(String [] args)
{ Scanner input = new Scanner(System.in);
int a;
int b;
int sum, quo, pro, dif;
System.out.print("enter the first number a= ");
a= input.nextInt();
System.out.print("enter the second number b= ");
b=input.nextInt();
sum=a+b;
que=a/b;
pro=a*b;
dif=a-b;
System.out.printf("the sum of %d and %d is %d%n",a,b, sum);
System.out.printf("the difference of %d and %d is %d%n",a,b, dif);
System.out.printf("the product of %d and %d is %d%n",a,b, pro);
System.out.printf("the quotient of %d divide by %d is %d%n", quo);
}
2.16 (Comparing Integers) Write an application that asks the user to enter two integers, obtains
them from the user and displays the larger number followed by the words "is larger". If the
numbers are equal, print the message "These numbers are equal".
import java.util.Scanner;
public class comparing {
public static void main (String [] args)
{
Scanner input = new Scanner (System.in);
int a, b;
System. out .print ("enter the first number a=");
a=input.nextInt();
System.out.print("enter the second number b=");
b=input.nextInt();
if(a>b)
System.out.printf("%d is larger", a);
if(b>a)
System.out.printf("%d is larger", b);
if(a==b)
System.out.printf("these numbers are equal");
4
}
}
2.17 (Arithmetic, Smallest and Largest) Write an application that inputs three integers from the
user and displays the sum, average, product, smallest and largest of the numbers. Use the
techniques shown in Fig. 2.15. [Note: The calculation of the average in this exercise should result
in an integer representation of the average. So, if the sum of the values is 7, the average should
be 2, not 2.3333….
import java.util.Scanner;
public class ArithmeticSmallestLargest
{
int a, b, c;
int largest, smallest;
int sum;
int product; int average;
largest = a;
if (b > largest)
largest = b;
if (c > largest)
largest = c;
smallest = c;
if (b < smallest)
smallest = b;
if (c < smallest)
smallest = c;
sum =a+ b+ c;
product = a * b * b;
average = sum / 3;
5
System.out.printf("Largest is %d\n", largest);
System.out.printf("Smallest is %d\n", smallest);
System.out.printf("Sum is %d\n", sum);
System.out.printf("Product is %d\n", product);
System.out.printf("Average is %d\n", average);
}
}
2.18 (Displaying Shapes with Asterisks) Write an application that displays a box, an oval,
an arrow
and a diamond using asterisks (*), as follows:
public class shapes {
public static void main( String args[] ) {
System.out.println( "********* *** * * " );
System.out.println( "* * * * *** * * " );
System.out.println( "* * * * ***** * * " );
System.out.println( "* ** * * * * " );
System.out.println( "* ** * * * *" );
System.out.println( "* ** * * * * " );
System.out.println( "* ** * * * * " );
System.out.println( "* * * * * * * " );
System.out.println( "********* *** * * " );
}
}
2.19 What does the following code print?
System.out.printf("*%n**%n***%n****%n*****%n");
Ans
*
**
***
****
*****
6
Ans
*
***
*****
****
**
**************
****
*****
******
7
*
***
*****
2.24 (Largest and Smallest Integers) Write an application that reads five integers and
determines and prints the largest and smallest integers in the group. Use only the programming
techniques you learned in this chapter.
import java.util.Scanner;
public class LargestSmallest {
public static void main( String args[] ){
Scanner input = new Scanner( System.in );
int a,b,c,d,e;
int largest,smallest;
System.out.print( "Enter first number a= " );
a = input.nextInt();
smallest=a;
largest=a;
System.out.print( "Enter first number b= " );
b = input.nextInt();
if(b<smallest)
smallest=b;
if(b>largest)
largest=b;
System.out.print( "Enter first number c= " );
c = input.nextInt();
if(c<smallest)
smallest=c;
if(c>largest)
largest=c;
System.out.print( "Enter first number d= " );
d = input.nextInt();
if(d<smallest)
smallest=d;
if(d>largest)
largest=d;
System.out.print( "Enter first number e= " );
e = input.nextInt();
if(e<smallest)
smallest=e;
if(e>largest)
largest=e;
System.out.printf( "Numbers input: %d %d %d %d %d\n\n", a, b, c, d, e);
System.out.printf( "Smallest number is: %d\n", smallest );
System.out.printf( "Largest number is: %d\n", largest );
}
}
8
2.25 (Odd or Even) Write an application that reads an integer and determines and prints
whether it’s odd or even. [Hint: Use the remainder operator. An even number is a multiple of 2.
Any multiple of 2 leaves a remainder of 0 when divided by 2.]
import java.util.Scanner;
public class odd_even {
public static void main (String [] args){
Scanner input = new Scanner( System.in );
int a;
System.out.print("enter a number to determine whether it is even or odd a= ");
a = input.nextInt();
if(a%2==0)
System.out.printf("%d is even",a);
if(a%2==1)
System.out.printf("%d is odd",a);}
}
2.26 (Multiples) Write an application that reads two integers, determines whether the first is
a multiple of the second and prints the result. [Hint: Use the remainder operator.]
import java.util.Scanner;
public class Multiples {
public static void main (String [] args)
{
Scanner input = new Scanner( System.in );
int a,b;
System.out.print("enter the first number a= ");
a = input.nextInt();
System.out.print("enter the second number b= ");
b = input.nextInt();
if(a%b==0)
System.out.printf("%d is the multiple of %d",a,b);
if(a%b!=0)
System.out.printf("%d is not the multiple of %d",a,b);
}
}
2.27 (Checkerboard Pattern of Asterisks) Write an application that displays a
checkerboard pattern, as follows:
********
********
********
********
********
********
********
********
9
System.out.println( " * * * * * * * *" );
System.out.println( "* * * * * * * *" );
System.out.println( " * * * * * * * *" );
System.out.println( "* * * * * * * *" );
System.out.println( " * * * * * * * *" );
System.out.println( "* * * * * * * *" );
System.out.println( " * * * * * * * *" );
}
2.28 (Diameter, Circumference and Area of a Circle) Write an application that inputs
from the user the radius of a circle as an integer and prints the circle’s diameter, circumference
and area using the floating-point value 3.14159 for π. Use the techniques shown in Fig. 2.7.
[Note: You may also use the predefined constant
Math.PI for the value of π. This constant is more precise than the value 3.14159. Class Math is
defined in package java.lang. Classes in that package are imported automatically, so you do not
need to import class Math to use it.] Use the following formulas (r is the radius):
diameter = 2r
circumference = 2πr
area = πr2
Do not store the results of each calculation in a variable. Rather, specify each calculation as the
value that will be output in a System.out.printf statement. The values produced by the
circumference and area calculations are floating-point numbers. Such values can be output with
the format specifier %f in a System.out.printf statement.
import java.util.Scanner;
public class Circle {
public static void main(String [] args)
{
Scanner input = new Scanner( System.in );
int r;
System.out.print( "Enter radius: " ); // prompt for input
r = input.nextInt(); // read number
}
}
2.29 Display the integer equivalents of the following: A B C a b c 0 1 2 $ * + / and the blank
character.
public class Integer_value_of_a_character {
10
public static void main( String args[] )
{
System.out.printf( "The character %c has the value %d\n",'A', ( (int) 'A' ));
System.out.printf( "The character %c has the value %d\n", 'B', ( (int) 'B' ) );
System.out.printf( "The character %c has the value %d\n", 'C', ( (int) 'C' ) );
System.out.printf( "The character %c has the value %d\n", 'a', ( (int) 'a' ) );
System.out.printf( "The character %c has the value %d\n", 'b', ( (int) 'b' ) );
System.out.printf( "The character %c has the value %d\n", 'c', ( (int) 'c' ) );
System.out.printf( "The character %c has the value %d\n",'0', ( (int) '0' ) );
System.out.printf( "The character %c has the value %d\n", '1', ( (int) '1' ) );
System.out.printf( "The character %c has the value %d\n", '2', ( (int) '2' ) );
System.out.printf( "The character %c has the value %d\n", '$', ( (int) '$' ) );
System.out.printf( "The character %c has the value %d\n", '*', ( (int) '*' ) );
System.out.printf( "The character %c has the value %d\n", '+', ( (int) '+' ) );
System.out.printf( "The character %c has the value %d\n", '/', ( (int) '/' ) ); System.out.printf(
"The character %c has the value %d\n", ' ', ( (int) ' ' ) );
}
}
2.30 (Separating the Digits in an Integer) Write an application that inputs one number
consisting of five digits from the user, separates the number into its individual digits and prints
the digits separated from one another by three spaces each. For example, if the user types in the
number 42339, the program should print
4 2 3 3 9
Assume that the user enters the correct number of digits. What happens when you enter a
number with more than five digits? What happens when you enter a number with fewer than
five digits?
import java.util.Scanner;
public class Digit {
public static void main( String [] args )
{
Scanner input = new Scanner( System.in );
int number;
int digit1;
int digit2;
int digit3;
int digit4;
int digit5;
11
}
}
2.31 (Table of Squares and Cubes) Using only the programming techniques you learned in
this chapter, write an application that calculates the squares and cubes of the numbers from 0 to
10 and prints the resulting values in table format, as shown below.
Number square cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
import java.util.Scanner;
public class SquareAndCubeTable {
public static void main( String [] args )
{
System.out.printf( "%s\t%s\t%s\n", "number", "square", "cube" );
int a = 0;
System.out.printf( "%d\t%d\t%d\n", a, ( a * a ), ( a * a * a ) );
a = 1;
System.out.printf( "%d\t%d\t%d\n", a, ( a * a ), ( a * a * a ) );
a = 2;
System.out.printf( "%d\t%d\t%d\n", a, ( a * a ), ( a * a * a ) );
a = 3;
System.out.printf( "%d\t%d\t%d\n", a, ( a * a ), ( a * a * a ) );
a = 4;
System.out.printf( "%d\t%d\t%d\n", a, ( a * a ), ( a * a * a ) );
a = 5;
System.out.printf( "%d\t%d\t%d\n", a, ( a * a ), ( a * a * a ) );
a = 6;
System.out.printf( "%d\t%d\t%d\n", a, ( a * a ), ( a * a * a ) );
a = 7;
System.out.printf( "%d\t%d\t%d\n", a, ( a * a ), ( a * a * a ) );
a = 8;
System.out.printf( "%d\t%d\t%d\n", a, ( a * a ), ( a * a * a ) );
a = 9;
System.out.printf( "%d\t%d\t%d\n", a, ( a * a ), ( a * a * a ) );
a = 10;
System.out.printf( "%d\t%d\t%d\n", a, ( a * a ), ( a * a * a ) );
}
}
12
2.32 (Negative, Positive and Zero Values) Write a program that inputs five numbers and
determines and prints the number of negative numbers input, the number of positive numbers
input and the number of zeros input.
import java.util.Scanner;
public class Tally
7{
8 public static void main( String args[] )
9{
10 Scanner input = new Scanner( System.in );
11
12 int inputNumber;
13 int zeroTally;
14 int positiveTally;
15 int negativeTally;
16
17 // initialize counters
18 zeroTally = 0;
19 positiveTally = 0;
20 negativeTally = 0;
21
22 System.out.print( "Enter first integer: " ); // prompt for input
23 inputNumber = input.nextInt(); // read first number
24
25 if ( inputNumber == 0 )
26 zeroTally = zeroTally + 1;
27
28 if ( inputNumber < 0 )
29 negativeTally = negativeTally + 1;
30
31 if ( inputNumber > 0 )
32 positiveTally = positiveTally + 1;
13
33
34 System.out.print( "Enter second integer: " ); // prompt for input
inputNumber = input.nextInt(); // read second number
37 if ( inputNumber == 0 )
38 zeroTally = zeroTally + 1;
39
40 if ( inputNumber < 0 )
41 negativeTally = negativeTally + 1;
42
43 if ( inputNumber > 0 )
44 positiveTally = positiveTally + 1;
45
46 System.out.print( "Enter third integer: " ); // prompt for input
47 inputNumber = input.nextInt(); // read third number
48
49 if ( inputNumber == 0 )
50 zeroTally = zeroTally + 1;
51
52 if ( inputNumber < 0 )
53 negativeTally = negativeTally + 1;
54
55 if ( inputNumber > 0 )
56 positiveTally = positiveTally + 1;
57
58 System.out.print( "Enter fourth integer: " ); // prompt for input
59 inputNumber = input.nextInt(); // read fourth number
60
61 if ( inputNumber == 0 )
62 zeroTally = zeroTally + 1;
63
14
64 if ( inputNumber < 0 )
65 negativeTally = negativeTally + 1;
66
67 if ( inputNumber > 0 )
68 positiveTally = positiveTally + 1;
69
70 System.out.print( "Enter fifth integer: " ); // prompt for input
71 inputNumber = input.nextInt(); // read fifth number
72
73 if ( inputNumber == 0 )
74 zeroTally = zeroTally + 1;
75
76 if ( inputNumber < 0 )
77 negativeTally = negativeTally + 1;
78
79 if ( inputNumber > 0 )
80 positiveTally = positiveTally + 1;
81
82 // create a string describing the results
83 System.out.printf( "\nThere are %d zeros\n", zeroTally );
84 System.out.printf( "There are %d positive numbers\n",
85 positiveTally );
86 System.out.printf( "There are %d negative numbers\n",
87 negativeTally );
88 } // end main
89 } // end class Tally
15