Introduction to programming in java Lecture 05 Review of 1 st four lectures and Practice Questions
Answer : D
Answer : A
Answer : C
What does the following program output? Answer : C
Answer : A
Answer : C
Answer : D
Answer : C
Answer : A
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 AreaOfTriangle{ public static void main(String args[]){ int b = Integer.parseInt(args[0]); // base of triangle int h = Integer.parseInt(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 RandomNumber{ 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: 1.Only HAND WRITTEN assignments are acceptable on A4 size white paper. 2.If any student copy assignment from other then -5 marks will be deducted from the overall grade. 3.Late submission is not allowed.
Homework Assignment # 1 (Cont…) Question 1: Write a program called MyInfo.java that prints your name, student ID and GPA on separate lines. Note use three variables name, studentID, 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( "bc"); – System.out.println((2+3) + "bc"); – System.out.println("bc" + (2+3)); – System.out.println("bc" ); Explain each outcome.
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:
Homework Assignment # 1 (Cont…) Question 4: Write a program called MaxNumber.java that takes two positive integers as command-line arguments and prints the maximum value. For example: Java MaxNumber 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.