Solution exercise 1 Scanner U new ScannerSystem in

  • Slides: 18
Download presentation
Solution exercise 1 Scanner U = new Scanner(System. in); System. out. println("Meters ? ");

Solution exercise 1 Scanner U = new Scanner(System. in); System. out. println("Meters ? "); double d = U. next. Double(); System. out. println("Seconds ? "); double t = U. next. Double(); System. out. println("velocity=" + (d / 1000) / (t / 3600) + "km/hour"); alternatively: System. out. println("velocity =" + 3. 6 * d / t + km/hour");

Solution exercise 2 Scanner U = new Scanner(System. in); System. out. println("In Armenia ?

Solution exercise 2 Scanner U = new Scanner(System. in); System. out. println("In Armenia ? "); double a = U. next. Double(); System. out. println("In Artsakh ? "); double b = U. next. Double(); System. out. println("In Diaspora ? "); double c = U. next. Double(); double factor = 100. 0 / (a + b + c); System. out. println("% Armenia=" + a * factor + "% "); System. out. println("% Artsakh=" + b * factor + "% "); System. out. println("% Diaspora=" + c * factor + "% ");

Solution Exercise 3 System. out. println("Two digits Number ? "); Scanner U = new

Solution Exercise 3 System. out. println("Two digits Number ? "); Scanner U = new Scanner(System. in); int n = U. next. Int(); int d 1 = n / 10, d 2 = n - d 1 * 10; System. out. println("Number with inverted digits " + d 2 + d 1); Equivalently: System. out. print("Number with inverted digits"); System. out. print(d 2); System. out. print(d 1); Solution with operator % int d 1 = n / 10, d 2 = n % 10; System. out. println("Number with inverted digits=" + d 2 + d 1); or directly: System. out. print("Number with inverted digits =" + n % 10 + n / 10);

Problem Write a program which implements the following dialog with the user: Perimeter and

Problem Write a program which implements the following dialog with the user: Perimeter and area of a triangle with sides a, b, c a ? ___ b ? ___ c ? ___ Perimeter = ? Area = ? Note. The area of a triangle can be computer with the formula s(s-a)(s-b)(s-c) with s=a+b+c (semi-perimeter) 2

Program System. out. println("perimeter and area of " + "a triangle with sides a,

Program System. out. println("perimeter and area of " + "a triangle with sides a, b, c"); // get sides Scanner U = new Scanner(System. in); System. out. print("a ? "); double a = U. next. Double(); System. out. print("b ? "); double b = U. next. Double(); System. out. print("c ? "); double c = U. next. Double(); // compute and show perimeter and area System. out. println("Perimeter = " + (a + b + c)); double s = (a + b + c) / 2; // semi-perimeter System. out. println("Area = " + Math. sqrt(s * (s - a) * (s - b) * (s - c))); // or Math. pow(s * (s - a) * (s - b) * (s - c), 0. 5);

Use (call) of pre-defined methods (fuctions) examples Math. sqrt(s*(s-a)*(s-b)*(s-c)) Math. pow (s*(s-a)*(s-b)*(s-c), 0. 5)

Use (call) of pre-defined methods (fuctions) examples Math. sqrt(s*(s-a)*(s-b)*(s-c)) Math. pow (s*(s-a)*(s-b)*(s-c), 0. 5) syntaxes Math. number(arguments) Arguments zero or more arithmetical expressions (separated by commas) semantic 1. evaluate arguments. Example: s*(s-a)*(s-b)*(s-c) 2. evaluate function (method) with given arguments 3. return result of function in the calling place

Pre-defined functions in the Java language (static methods of the pre-defined Math class) •

Pre-defined functions in the Java language (static methods of the pre-defined Math class) • all functions get and return a double • a double argument admits an int function sqrt(x) pow(x, y) exp(x) log(x) sin(x) meaning x, x 0 xy ex logex Sin of angle x example sqrt(4. 0) pow(2. 0, 3. 0) exp(1. 0) log(Math. E) sin(Math. PI) result 2. 0 8. 0 2. 71. . . 1. 0 0. 0 cos(x) tan(x) asin(x) acos(x) atan(x) Cosin of x Tangent of x arc-sin of x arc-cosin of x arc-tangent x cos(Math. PI) tan(Math. PI) asin(1. 0) acos(1. 0) atan(0. 0) -1. 0 0. 0 Π/2 0. 0

Functions defined by the programmer import java. util. *: class Program{ //function for computing

Functions defined by the programmer import java. util. *: class Program{ //function for computing the perimeter of a triangle static public double perimeter(double x, double y, double z){ return x+y+z; } //function for computing the area of a triangle static public double area(double x, double y, double z){ double s=perimeter(x, y, z)/2; return Math. sqrt(s*(s-x)*(s-y)*(s-z)); } //user program static public void main(String[]args){ System. out. println(“perimeter and area of a triangle”); System. out. println(“a b c ? ”); double a = U. next. Double(), b = U. next. Double(), c = U. next. Double(); System. out. println(“perimeter=” + perimeter(a, b, c)); System. out. println(“area=” + area(a, b, c)); } }

Functions defined by the programmer syntaxes static public result-type number(parameters) { instructions return expression;

Functions defined by the programmer syntaxes static public result-type number(parameters) { instructions return expression; } parameters? type name, . . . semantic 1. copy (assign) calling arguments to parameters 2. perform instructions (if any) 3. return value of expression at calling place, Respecting result type

Solution 2. With user-defined functions class Program { static public void main(String[]args){ int i=rint(0,

Solution 2. With user-defined functions class Program { static public void main(String[]args){ int i=rint(0, 9), j=rint(0, 9), k=rint(0, 9); System. out. println(“Biggest=“ +max 3(i, j, k)); } //generates random integer between x and y static public int rint(int x, int y){ return x + (int)(Math. random()*(y-x+1)); } //returns the biggest from x, y, z static public int max 3(int x, int y, int z){ return Math. max(x, y), z); } }

Exercise. Complete the class to order 3 random integers between 1 and 100. The

Exercise. Complete the class to order 3 random integers between 1 and 100. The program should write the results as shown in the following example: Numbers = 43 72 28 Ordered = 28 43 72 class Program{ static public void main(String[]args){ … } static public int rint(int x, int y){ return x + (int)(Math. random()*(y-x+1)); } static public int max(int x, int y, int z){ return Math. max(x, y), z ); } static public int min(int x, int y, int z){ … } static public int middle(int x, int y, int z){ … } }

Use of functions in other classes class Program { public static void main(String[] args)

Use of functions in other classes class Program { public static void main(String[] args) { int a = C. random(1, 100), b = C. random(1, 100), c = C. random(1, 100); System. out. println("Numbers=" + a + " " + b + " " + c); System. out. println("Ordered=" + C. min(a, b, c) + " " + C. middle(a, b, c) + " " + C. max(a, b, c)); } } Syntaxes name. Class. name. Function(arguments)