Introduction to programming in java Lecture 05 Review

  • Slides: 18
Download presentation
Introduction to programming in java Lecture 05 Review of 1 st four lectures and

Introduction to programming in java Lecture 05 Review of 1 st four lectures and Practice Questions

Answer : D

Answer : D

Answer : A

Answer : A

Answer : C

Answer : C

What does the following program output? Answer : C

What does the following program output? Answer : C

Answer : A

Answer : A

Answer : A

Answer : A

Answer : C

Answer : C

Answer : D

Answer : D

Answer : C

Answer : C

Answer : A

Answer : A

Practice Questions • Program 1: Write a program that takes base and height of

Practice Questions • Program 1: Write a program that takes base and height of a triangle from the command line arguments and prints the area of triangle. • Program 2: Write a program that prints the random value between 0 and 100.

Solution: Program 1 class Area. Of. Triangle{ public static void main(String args[]){ int b

Solution: Program 1 class Area. Of. Triangle{ public static void main(String args[]){ int b = Integer. parse. Int(args[0]); // base of triangle int h = Integer. parse. Int(args[1]); // height of trangle int area = (½)*b * h; System. out. println(“base : ” + b); System. out. println(“height : ” + h); System. out. println(“Area of triangle: ” + area); } }

Solution: Program 2 class Random. Number{ public static void main(String args[]) { double r

Solution: Program 2 class Random. Number{ public static void main(String args[]) { double r = Math. random(); // uniform between 0 and 1 int num = (int)(r*100); // uniform between 0 and 100 System. out. println(“Random number: ” + num); } }

Homework Assignment # 1 (Total marks: 5) Submission deadline: 24 th Sep 2013 NOTES:

Homework Assignment # 1 (Total marks: 5) Submission deadline: 24 th Sep 2013 NOTES: 1. 2. 3. Only HAND WRITTEN assignments are acceptable on A 4 size white paper. If any student copy assignment from other then -5 marks will be deducted from the overall grade. Late submission is not allowed.

Homework Assignment # 1 (Cont…) Question 1: Write a program called My. Info. java

Homework Assignment # 1 (Cont…) Question 1: Write a program called My. Info. java that prints your name, student ID and GPA on separate lines. Note use three variables name, student. ID, and GPA and assign proper values to them. Question 2: What do each of the following print? – – – System. out. println(2 + "bc"); System. out. println(2 + 3 + "bc"); System. out. println((2+3) + "bc"); System. out. println("bc" + (2+3)); System. out. println("bc" + 2 + 3); Explain each outcome.

Homework Assignment # 1 (Cont…) Question 3: Write a program that averages the rain

Homework Assignment # 1 (Cont…) Question 3: Write a program that averages the rain fall for three months, April, May, and June. Declare and initialize a variable to the rain fall for each month. Compute the average, and write out the results, something like: Rainfall for April: 12 Rainfall for May : 14 Rainfall for June: 8 Average rainfall: 11. 333333

Homework Assignment # 1 (Cont…) Question 4: Write a program called Max. Number. java

Homework Assignment # 1 (Cont…) Question 4: Write a program called Max. Number. java that takes two positive integers as command-line arguments and prints the maximum value. For example: Java Max. Number 9 4 Maximum value: 9 Hint: you need to use Math. max() function (See lecture 3, slide 21). Question 5: Write a program that solves the quadratic equation x 2 + bx + c = 0. Note that, pass the values of b and c as a command line argument to the program. Formula for solving quadratic equation is given below.