Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion 15.3.2014-20.3.2014



Similar documents
Sample CSE8A midterm Multiple Choice (circle one)

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

J a v a Quiz (Unit 3, Test 0 Practice)

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Building Java Programs

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Factoring Polynomials and Solving Quadratic Equations

Interactive Applications (CLI) and Math

ALGEBRA 2: 4.1 Graph Quadratic Functions in Standard Form

Introduction to Java

Part I. Multiple Choice Questions (2 points each):

1.3 Algebraic Expressions

AP Computer Science Static Methods, Strings, User Input

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Chapter 8 Selection 8-1

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 2 Introduction to Java programming

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

What are the place values to the left of the decimal point and their associated powers of ten?

3.2. Solving quadratic equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

Conditionals (with solutions)

7.1 Graphs of Quadratic Functions in Vertex Form

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Topic 11 Scanner object, conditional execution

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Zero: If P is a polynomial and if c is a number such that P (c) = 0 then c is a zero of P.

Programming Languages CIS 443

AP Computer Science Java Subset

2009 Tutorial (DB4O and Visual Studio 2008 Express)

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Conditional Statements Summer 2010 Margaret Reid-Miller

Java Basics: Data Types, Variables, and Loops

Chapter 2: Elements of Java

PYTHAGOREAN TRIPLES KEITH CONRAD

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Building Java Programs

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Object Oriented Software Design

Building Java Programs

Introduction to Programming

4.1. COMPLEX NUMBERS

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

CS 241 Data Organization Coding Standards

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Math Review. for the Quantitative Reasoning Measure of the GRE revised General Test

1001ICT Introduction To Programming Lecture Notes

SECTION 2.5: FINDING ZEROS OF POLYNOMIAL FUNCTIONS

SOLVING QUADRATIC EQUATIONS - COMPARE THE FACTORING ac METHOD AND THE NEW DIAGONAL SUM METHOD By Nghi H. Nguyen

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

ALGORITHMS AND FLOWCHARTS. By Miss Reham Tufail

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Copyrighted Material. Chapter 1 DEGREE OF A CURVE

NSM100 Introduction to Algebra Chapter 5 Notes Factoring

Lagrange Interpolation is a method of fitting an equation to a set of points that functions well when there are few points given.

MA107 Precalculus Algebra Exam 2 Review Solutions

Chapter 5. Recursion. Data Structures and Algorithms in Java

Zeros of a Polynomial Function

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Building Java Programs

Section 3.1 Quadratic Functions and Models

Introduction to Java Lecture Notes. Ryan Dougherty

Algebra Practice Problems for Precalculus and Calculus

Basic Programming and PC Skills: Basic Programming and PC Skills:

Example of a Java program

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Users Guide to the Ecliptic Calendar by Damon Scott, Author of the Ecliptic System

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Lecture 8 : Coordinate Geometry. The coordinate plane The points on a line can be referenced if we choose an origin and a unit of 20

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Elements of a graph. Click on the links below to jump directly to the relevant section

Star signs and horoscopes

MATH 21. College Algebra 1 Lecture Notes

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

CONVERT QUADRATIC FUNCTIONS FROM ONE FORM TO ANOTHER (Standard Form <==> Intercept Form <==> Vertex Form) (By Nghi H Nguyen Dec 08, 2014)

Factoring Quadratic Expressions

Comp 248 Introduction to Programming

Chapter 2 Elementary Programming

Java Crash Course Part I

Vector Notation: AB represents the vector from point A to point B on a graph. The vector can be computed by B A.

Moving from CS 61A Scheme to CS 61B Java

The following program is aiming to extract from a simple text file an analysis of the content such as:

CS170 Lab 11 Abstract Data Types & Objects

1.1 Your First Program

Building Java Programs

FACTORING QUADRATICS and 8.1.2

Part I. The Picture class

Statements and Control Flow

Transcription:

German University in Cairo Media Engineering and Technology Prof. Dr. Slim Abdennadher Dr. Ahmed Gamal Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion 15.3.2014-20.3.2014 Exercise 3-1 To be discussed in the tutorial Code Refactoring Refactor each of the following program fragments (if possible) Program 1 if (x > y) { else { System.out.println( x is less than or equal to y ); We can first extract to back, thus we get: if (x > y) { else { System.out.println( x is less than or equal to y ); Then, we can apply swap branches, we get: if (!(x > y)) { //negating the condition System.out.println( x is less than or equal to y ); else { We cannot do any more refactoring steps. However, it can be simplified to the following program: if (x <= y) { System.out.println( x is less than or equal to y ); Program 2 1

if (x == 0) { System.out.println( x is even ); if ( x % 2 == 0) { System.out.println( x is even ); if (x == 1) { System.out.println( x is odd ); if (x % 2!= 0) { System.out.println( x is odd ); The program cannot be refactored because : a) We cannot apply swap branches since we do not have if-else statements. b) We cannot apply remove redundant tests because there is no condition repeated twice. c) We cannot apply extract to back or extract to front because there are no if-else statements. However, the program can be simplified to: if (x % 2 == 0) { System.out.println( x is even ); else { System.out.println( x is odd ); Exercise 3-2 To be discussed in the tutorial Cartesian Plane Write a Java program that reads the x-y coordinates of a point in the Cartesian plane and displays a message telling the user the quadrant in which the point is located or the axis on which the point lies. 2

public class CartesainPlane { double x,y; System.out.print("Please enter the x-coordinate of the point "); x = sc.nextint(); System.out.print("Please enter the y-coordinate of the point "); y = sc.nextint(); if ((x == 0) && (y == 0)) { System.out.println("The point is the origin"); else if (x == 0) { System.out.println("The point lies on the y-axis"); else if (y == 0) { System.out.println("The point lies on the x-axis"); else if (x < 0) { if (y > 0 ) { System.out.println("The point lies in the second quadrant"); else { System.out.println("The point lies in the third quadrant"); else { if (y > 0 ) { System.out.println("The point lies in the first quadrant"); else { System.out.println("The point lies in the forth quadrant"); 3

Exercise 3-3 Maximum Write a Java program to calculate the maximum of three numbers. Solve using conditional operator only. public class Maximum { int num1, num2, num3, max; System.out.print("Please enter the first number: "); num1 = sc.nextint(); System.out.print("Please enter the second number: "); num2 = sc.nextint(); System.out.print("Please enter the third number: "); num3 = sc.nextint(); max = num1 > num2? (num1 > num3? num1 : num3 ) : (num2 > num3? num2 : num3 ); System.out.println("Maximum = " + max); Exercise 3-4 To be discussed in the tutorial Months Write a Java program that prints the number of days for any given month. Please enter the month number (1-12): 5 31 days. public class Month { int month; String numberofdays; System.out.print("Please enter the month number (1-12): "); month = sc.nextint(); switch (month) { case 1: case 3: 4

case 5: case 7: case 8: case 10: case 12: numberofdays = "31 days."; case 4: case 6: case 9: case 11: numberofdays = "30 days."; case 2: numberofdays = "either 28 or 29 days."; default: numberofdays = "The value you entered for the month is not correct!"; System.out.println(numberofdays); Exercise 3-5 To be discussed in the lab Quadratic Equation Write a Java program that reads from the user three double numbers a, b, c representing the coefficients of a quadratic equation ax 2 +bx+c = 0. The program should calculate the roots of the quadratic equation using the following formulae: x 1 = b+ (b 2 4ac) 2a x 2 = b (b 2 4ac) 2a If a = 0 or if b 2 4ac < 0 the output of the program should be: No Solutions! public class Quadratic { double a,b,c,underroot; System.out.println("Please enter the coefficient a"); a = sc.nextdouble(); 5

System.out.println("Please enter the coefficient b"); b = sc.nextdouble(); System.out.println("Please enter the coefficient c"); c = sc.nextdouble(); underroot = Math.pow(b,2) - (4*a*c); if ((a == 0) (underroot < 0)) { System.out.println("No Solution!"); else if (underroot == 0) { x1 = -b/(2*a); x2 = x1; System.out.println("X1 = X2 = "+x1 ); else { x1 = (-b + Math.sqrt(underroot))/(2*a); x2 = (-b - Math.sqrt(underroot))/(2*a); System.out.println("X1 = "+x1); System.out.println("X2 = "+x2); Exercise 3-6 To be discussed in the lab Calculator Write a Java program that designs a simple calculator. The program should read two rational numbers and a character that indicates the type of operation desired. Those operations include addition, subtraction, multiplication, division and calculating remainder. Solve using switch statement only. public class Calculator { public static void main(string args[]) { double operand1, operand2, result; char operator; System.out.print("Please type in the first operand: "); operand1 = sc.nextdouble(); System.out.print("Please type in the second operand: "); operand2 = sc.nextdouble(); System.out.print("Please type in the first character of the operation you want: "); operator = sc.next().charat(0) ; switch (operator) { case a : case A : result = operand1 + operand2; System.out.println(operand1 + " + " + operand2 +" = "+ result); case s : case S : result = operand1 - operand2; 6

System.out.println(operand1 + " - " + operand2 +" = "+ result); case m : case M : result = operand1 * operand2; System.out.println(operand1 + " * " + operand2 +" = "+ result); case d : case D : result = operand1 / operand2; System.out.println(operand1 + " / " + operand2 +" = "+ result); case r : case R : result = operand1 % operand2; System.out.println(operand1 + " % " + operand2 +" = "+ result); default: System.out.println("There is no operation corresponding to this input!"); Exercise 3-7 To be discussed in the lab Zodiac Write a Java program that requests a month number (1-12) and a day number (1-31). The program should print the Zodiac Sign according to the user s input. Sign From To Capricorn December 22 January 19 Aquarius January 20 February 17 Pisces February 18 March 19 Aries March 20 April 19 Taurus April 20 May 20 Gemini May 21 June 20 Cancer June 21 July 22 Leo July 23 August 22 Virgo August 23 September 22 Libra September 23 October 22 Scorpio October 23 November 21 Sagittarius November 22 December 21 public class Zodiac { 7

int month, day; String horoscope; System.out.print("Please enter the month(1-12): "); month = sc.nextint(); System.out.print("Please enter the day(1-31): "); day = sc.nextint(); switch (month) { case 1: horoscope = (day <= 19)? "Capricorn" : "Aquarius"; case 2: horoscope = (day <= 17)? "Aquarius" : "Pisces"; case 3: horoscope = (day <= 19)? "Pisces" : "Aries"; case 4: horoscope = (day <= 19)? "Aries" : "Taurus"; case 5: horoscope = (day <= 20)? "Taurus" : "Gemini"; case 6: horoscope = (day <= 20)? "Gemini" : "Cancer"; case 7: horoscope = (day <= 22)? "Cancer" : "Leo"; case 8: horoscope = (day <= 22)? "Leo" : "Virgo"; case 9: horoscope = (day <= 22)? "Virgo" : "Libra"; case 10: horoscope = (day <= 22)? "Libra" : "Scorpio"; case 11: horoscope = (day <= 21)? "Scorpio" : "Sagittarius"; case 12: horoscope = (day <= 21)? "Sagittarius" : "Capricorn"; default: horoscope = "The value you entered for the month is not correct!"; System.out.print(horoscope); 8

Exercise 3-8 Tire s Pressure Write a program that reads in the pressure of the four tires and writes a message that says if the inflation is OK or not. Tires don t have to have exactly the same pressure. The front tires can be within 3 psi of each other, and the rear tires can be within 3 psi of each other. You must make sure that each tire has a pressure between 35 and 45. Input right front pressure : 35 Input left front pressure : 37 Input right rear pressure : 41 Input left rear pressure : 44 Inflation is OK public class Tire { ; int leftfront, rightfront, leftrear, rightrear; System.out.print ("Input the right front pressure : "); rightfront = sc.nextint(); System.out.print ("Input the left front pressure : "); leftfront = sc.nextint(); System.out.print ("Input the right rear pressure : "); rightrear = sc.nextint(); System.out.print ("Input the left rear pressure : "); leftrear = sc.nextint(); if((rightfront >= 35) && (rightfront <= 45 ) && (leftfront >= 35) && (leftfront <= 45) && (rightrear >= 35) && (rightrear <= 45) && (leftrear >= 35) && (leftrear <= 45 ) && ((rightfront-leftfront <= 3) (rightfront-leftfront >= -3 )) && ((rightrear-leftrear <= 3) (rightrear-leftrear >= -3 ))){ System.out.println("The inflation is OK."); else { System.out.println("Problem with inflation!"); 9