Computer Programming Lab 8.

Slides:



Advertisements
Similar presentations
Solve problems with Java code Algorithms with Java.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Introduction to Programming
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan
Introduction to Programming
Methods and Formatted Output Chapter 4. Overview Breaking down a big program into small methods. Formatting output – using printf – using DecimalFormat.
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
1 Lecture 16/3/11: Contents Example of an array of user-defined objects: Car and Carr Constructor Providing functionalities for a user- defined object.
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Programming Methodology (1). Implementing Methods main.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.
Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
CS110 Programming Language I
1 Various Methods of Populating Arrays Randomly generated integers.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Building Java Programs
CS1010 Programming Methodology
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Computer Programming Lab(4).
Computer Programming Lab(5).
Chapter 5 Case Study. Chapter 5 The RPS Flowchart.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Lecture 8 Using casts, Strings and WordUtil. Agenda Generating random numbers Casts – Casting a double into an int – Casting an int into a char – Casting.
Mixing integer and floating point numbers in an arithmetic operation.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Computer Programming Lab 6. Exercise 1 Sample Runs.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
CSC111 Quick Revision.
Introduction to programming in java
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
Building Java Programs
Building Java Programs
Chapter 1 Introduction to Java
Building Java Programs
Maha AlSaif Maryam AlQattan
Computer Programming Methodology Input and While Loop
User input We’ve seen how to use the standard output buffer
Introduction to Methods in java
SELECTION STATEMENTS (1)
Something about Java Introduction to Problem Solving and Programming 1.
BIT115: Introduction to Programming
Control Statement Examples
Conditional Branching
Building Java Programs
Introduction to Classes and Methods
Building Java Programs
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Building Java Programs
Building Java Programs
Pseudo-Code Conditional Branches
Computer Science Club 1st November 2019.
Presentation transcript:

Computer Programming Lab 8

Exercise 1 (Use the &&, || and ^ operators) Write a program that prompts the user to enter an integer and determines whether it is divisible by 5 and 6, whether it is divisible by 5 or 6, and whether it is divisible by 5 or 6, but not both. Here is a sample run of this program:

Source Code package Exersise1; import java.util.Scanner; public class Exersise1{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); int x = input.nextInt(); System.out.println(((x % 5 == 0)&& (x % 6 == 0 ))? x + " is divisible by 5 and 6? True " : x + " is divisible by 5 and 6? false "); System.out.println(((x % 5 == 0)||(x % 6 == 0 ))? x + " is divisible by 5 or 6? True " : x + " is divisible by 5 or 6? false "); System.out.println(((x % 5 == 0)^(x % 6 == 0 ))? x + " is divisible by 5 and 6 put not both? True " : x + " is divisible by 5 and 6 put not both ? false ");}}

Exercise 2

Source Code package Exercise2; import java.util.Scanner; public class Exercise2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int computerNumber = (int)(Math.random() * 3); System.out.print("scissor (0), rock (1), paper (2): "); int userNumber = input.nextInt(); switch (computerNumber) { case 0: if (userNumber == 0) System.out.print("The computer is scissor. You are scissor too. It is a draw"); else if (userNumber == 1) System.out.print("The computer is scissor. You are rock. You won"); else if (userNumber == 2) System.out.print("The computer is scissor. You are paper. You lost"); break;

case 1: if (userNumber == 0) System. out. print("The computer is rock case 1: if (userNumber == 0) System.out.print("The computer is rock. You are scissor. You lost"); else if (userNumber == 1) System.out.print("The computer is rock. You are rock too. It is a draw"); else if (userNumber == 2) System.out.print("The computer is rock. You are paper. You won"); break; case 2: System.out.print("The computer is paper. You are scissor. You won"); System.out.print("The computer is paper. You are rock. You lost"); System.out.print("The computer is paper. You are paper too. It is a draw"); break;} }}

Exercise 3 (Financials: currency exchange) Write a program that prompts the user to enter the exchange rate from currency in U.S. dollars to Saudi riyal SR or vice . Prompt the user to enter 0 to convert from U.S. dollars to Saudi riyal SR and 1 to convert from Saudi riyal SR and U.S. dollars. Prompt the user to enter the amount in U.S. dollars or Saudi riyal SR to convert it to Saudi riyal SR or U.S. dollars, respectively. Here are the sample runs:

Source Code : package exercise3; import java.util.Scanner; public class Exercise3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("enter The amount of monye "); int amount=input.nextInt(); System.out.println("Select 0 to convert it to U.S. dollars , and 1 to convert it to Saudi riyal SR "); int choice=input.nextInt(); double result; switch (choice) { case 0: result= (amount/3.75); System.out.printf ( "%d SR equal to %f $",amount,result); break; case 1: result=(amount*3.75); System.out.printf ( "%d $ equal to %f SR",amount,result); break ; } }