Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);

Slides:



Advertisements
Similar presentations
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Advertisements

CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
CSCI1402: Lecture 1 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
JAVA Control Statement.
Computer Programming Lab(4).
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Starting Out with Java: From Control Structures through Objects
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Random numbers. 2 The Random class A Random object generates pseudo-random numbers. –Class Random is found in the java.util package. import java.util.*;
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Decisions, Decisions, Decisions Conditional Statements In Java.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Application development with Java Lecture 6 Rina Zviel-Girshin.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
CS1101: Programming Methodology Preparing for Practical Exam (PE)
1 Control Structures (Chapter 3) 3 constructs are essential building blocks for programs Sequences  compound statement Decisions  if, switch, conditional.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Control Statements: Part1  if, if…else, switch 1.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Chapter 2 Clarifications
Introduction to programming in java
Building Java Programs
TK1114 Computer Programming
SELECTION STATEMENTS (1)
Control Statement Examples
Building Java Programs
Building Java Programs
Self study.
Building Java Programs
Building Java Programs
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
PROGRAM FLOWCHART Selection Statements.
Repetition Statements
Building Java Programs
Presentation transcript:

Programming Methodology (1)

import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in); double price, tax; System.out.println("*** Product Price Check ***"); System.out.print("Enter initial price: "); price = sc.nextDouble(); System.out.print("Enter tax rate: "); tax = sc.nextDouble(); price = price * (1 + tax/100); System.out.println("Cost after tax = " + price); }

RUN *** Product Price Check *** Enter initial price: _ 1000 Enter tax rate: _12.5 Cost after tax =

import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in); double price, tax; System.out.println("*** Product Price Check ***"); System.out.print("Enter initial price: "); price = sc.nextDouble(); System.out.print("Enter tax rate: "); tax = sc.nextDouble(); price = price * (1 + tax/100); System.out.println("Cost after tax = " + price); }

Selection

Learning objectives explain the difference between sequence and selection; use an if statements to make a single choice in a program; use an if...else statement to make a choice between two options in a program; use nested if…else statements to make multiple choices in a program; use a switch statement to make multiple choices in a program.

Making choices

display the price of the seats requested ? display a list of alternative flights ? display a message saying that no flights are available to that destination ?

Selection in Java if statement if…else statement switch statement

The ‘'if’' statement if ( ) { } // some code here // some code here // some code here /* a test goes here */  question

The ‘'if’' statement: an example if ( ) { } temperature = sc.nextDouble(); System.out.println(“Below freezing”); System.out.print(“Enter another temperature”); temperature < 0 -5 Below freezing Enter another temperature Enter another temperature RUN 12

Remember: you can have multiple instructions inside an if statement!

import java.util.*; public class FindCostWithDiscount { public static void main(String[] args ) { double price, tax; Scanner sc = new Scanner(System.in); System.out.println("*** Product Price Check ***"); System.out.print("Enter initial price: "); price = sc.nextDouble(); System.out.print("Enter tax rate: "); tax = sc.nextDouble(); price = price * (1 + tax/100); System.out.println("Cost after tax = " + price); } } // code to reduce tax here if price is greater than 100 if ( ) { } price > 100 System.out.println (“Special promotion: Your tax will be halved!“); tax = tax * 0.5;

*** Product Price Check *** Enter initial price: 50 Enter tax rate: 10 Cost after tax = 55.0

*** Product Price Check *** Enter initial price: 1000 Enter tax rate: 10 Special Promotion: Your tax will be halved! Cost after tax =

The comparison operators of Java OperatorMeaning ==equal to !=not equal to <less than >greater than >=greater than or equal to <=less than or equal to if ( temperature >= 18 ) { System.out.println("Today is a hot day!"); }

The comparison operators of Java OperatorMeaning ==equal to !=not equal to <less than >greater than >=greater than or equal to <=less than or equal to if (angle == 90) { System.out.println("This is a right angle!"); }

The comparison operators of Java OperatorMeaning ==equal to !=not equal to <less than >greater than >=greater than or equal to <=less than or equal to if (angle != 90) { System.out.println("This is NOT a right angle!"); }

The ‘'if…else’' statement if (/* a test goes here */ ) { } // some code here // some code here // some code here  // some code here else { }

if ( ) { } mark = sc.nextInt(); System.out.println("“You have passed!"); System.out.println("“You have failed!");  System.out.println("“Good luck with other exams"); else { } mark >= 50

import java.util.*; public class DisplayResult { public static void main(String[ ] args) { int mark; Scanner sc = new Scanner(System.in); System.out.println("What exam mark did you get? "); mark = sc.nextInt(); if (mark >= 50) { System.out.println("Congratulations, you passed"); } else { System.out.println("I'm sorry, but you failed"); } System.out.println("Good luck with your other exams"); } }

What exam mark did you get? _

What exam mark did you get? 52 Congratulations, you passed Good luck with your other exams

What exam mark did you get? _

35 Im sorry, but you failed Good luck with your other exams

Combining tests

5 Celsius TEMPERATURE 12 Celsius

if ( )temperature >= 5temperature <= 12&&

Logical operatorJava counterpart AND OR NOT && || !

if ( )temperature >= 18! ( )

Nested ‘if…else’ statements

grouptime A10.00 a.m. B1.00 p.m. C11.00 a.m

if (group == 'A') { } else { } if (group == 'B') { } else { } if (group == 'C') { } else { } System.out.print("No such group"); System.out.print("1.00 p.m"); System.out.print("11.00 a.m"); System.out.print("10.00 a.m");

if (group == 'A') { } else { } if (group == 'B') { } else { } if (group == 'C') { } else { } System.out.print("No such group"); System.out.print("1.00 p.m"); System.out.print("11.00 a.m"); System.out.print("10.00 a.m"); if (group == 'A') { System.out.print("10.00 a.m"); } else if (group == 'B') { System.out.print("1.00 p.m"); } else if (group == 'C') { System.out.print("11.00 a.m"); } else { System.out.print("No such group"); }

The 'switch' statement

switch( ) { } someVariable casevalue1:// instructions(s) to be executed break; case value2:// instructions(s) to be executed break; // more values to be tested can be added default:// instruction(s) for default case

The 'switch' statement: An example….

grouptime A10.00 a.m. B1.00 p.m. C11.00 a.m

switch( ) { } someVariable casevalue1:// instructions(s) to be executed break; casevalue1:// instructions(s) to be executed break; // more values to be tested can be added default:// instruction(s) for default case group ‘A’:System.out.print("10.00 a.m"); ‘B’:System.out.print("1.00 p.m"); case ‘C’:System.out.print("11.00 a.m"); break; System.out.print(“No such group.");

switch( ) { } group case‘A’:System.out.print("10.00 a.m"); break; case‘B’:System.out.print("1.00 p.m"); break; case ‘C’: default:System.out.print(“No such group."); System.out.print("11.00 a.m");break; a.m1.00 p.m11.00 a.mNo such group No such group11.00 a.m

Combining options grouptime A10.00 a.m. B1.00 p.m. C11.00 a.m

Combining options grouptime A10.00 a.m. B1.00 p.m. C10.00 a.m

switch(group) { case 'A': System.out.print("10.00 a.m "); break; case 'B': System.out.print("1.00 p.m "); break; case ‘C’: System.out.print("10.00 a.m "); break; default: System.out.print("No such group"); }

switch(group) { case 'A': System.out.print("10.00 a.m "); break; case ‘C’: System.out.print("10.00 a.m "); break; case 'B': System.out.print("1.00 p.m "); break; default: System.out.print("No such group"); } switch(group) { case 'A': case ‘C’: System.out.print("10.00 a.m "); break; case 'B': System.out.print("1.00 p.m "); break; default: System.out.print("No such group"); }

import java.util.*; public class SelectionQ4 { public static void main(String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _ Red 10

import java.util.*; public class SelectionQ4 { public static void main(String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _ Green 20 Blue Red

import java.util.*; public class SelectionQ4 { public static void main(String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _10 Blue Red

import java.util.*; public class SelectionQ5 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); } else { System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _10 Blue Red

import java.util.*; public class SelectionQ5 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); } else { System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _20 Green Red

import java.util.*; public class SelectionQ6 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); switch (x) { case 1: case 2: System.out.println("Green"); break; case 3: case 4: case 5: System.out.println("Blue"); break; default: System.out.println("numbers 1-5 only"); } System.out.println(“Red"); } Enter a number: _1 Green Red

import java.util.*; public class SelectionQ6 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); switch (x) { case 1: case 2: System.out.println("Green"); break; case 3: case 4: case 5: System.out.println("Blue"); break; default: System.out.println("numbers 1-5 only"); } System.out.println(“Red"); } Enter a number: _2 Green Red

import java.util.*; public class SelectionQ6 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); switch (x) { case 1: case 2: System.out.println("Green"); break; case 3: case 4: case 5: System.out.println("Blue"); break; default: System.out.println("numbers 1-5 only"); } System.out.println(“Red"); } Enter a number: _3 Blue Red

import java.util.*; public class SelectionQ6 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); switch (x) { case 1: case 2: System.out.println("Green"); break; case 3: case 4: case 5: System.out.println("Blue"); break; default: System.out.println("numbers 1-5 only"); } System.out.println(“Red"); } Enter a number: _10 numbers 1-5 only Red

import java.util.*; public class SelectionQ6 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); switch (x) { case 1: case 2: System.out.println("Green"); break; case 3: case 4: case 5: System.out.println("Blue"); break; default: System.out.println("numbers 1-5 only"); } System.out.println(“Red"); } Enter a number: _10 Red

Design and implement a program that asks the user to enter two numbers and then guess at the sum of those two numbers. If the user guesses correctly a congratulatory message is displayed, otherwise a commiseration message is displayed along with the correct answer.

Enter first number _

Enter first number 12 Enter second number _

12 Enter second number 6 Guess the sum _

Enter first number 12 Enter second number 6 Guess the sum 18 Correct answer!

Enter first number _

Enter first number 12 Enter second number _

12 Enter second number 6 Guess the sum _

Enter first number 12 Enter second number 6 Guess the sum 14 Wrong answer! = 18