Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

Similar documents
CAT.woa/wa/assignments/eclipse

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

Lab Exercise 1. Objectives: Part 1. Introduction

Objectives: Lab Exercise 1 Part 1. Sample Run. Part 2

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

b. Suppose you enter input from the console, when you run the program. What is the output?

Midterm Examination (MTA)

LAB 12: ARRAYS (ONE DIMINSION)

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Example Program. public class ComputeArea {

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Instructions. Objectives:

Chapter 5 Lab Methods

Chapter 3: Classes and Objects. CSC2010 Lab 9. Java Programming. Copyright 2000 W. W. Norton & Company. All rights reserved. FROM THE BEGINNING

Oct Decision Structures cont d

Assignment: TwoMethods Submitted to WebCat

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

AP Computer Science Java Mr. Clausen Program 6A, 6B

Tutorial 03. Exercise 1: CSC111 Computer Programming I

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

Instructions. Objectives:

Ahmadu Bello University Department of Mathematics First Semester Examinations June 2014 COSC211: Introduction to Object Oriented Programming I

Reading Input from Text File

PROGRAM: import java.util.scanner;

Objects and Classes User-defined classes, creating objects, accessing object s members

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

What two elements are usually present for calculating a total of a series of numbers?

Fundamentals of Programming Data Types & Methods

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Chapter 4: Control Structures I

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

Full file at

ITERATION WEEK 4: EXMAPLES IN CLASS

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Final Examination Semester 3 / Year 2012

Object Oriented Programming. Java-Lecture 6 - Arrays

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

Object Oriented Programming with JAVA

Building Java Programs

Loops. GEEN163 Introduction to Computer Programming

Arrays. Eng. Mohammed Abdualal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Exceptions Handeling

Array. Array Declaration:

CS141 Programming Assignment #6

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Exercise 1: Class Employee: public class Employee { private String firstname; private String lastname; private double monthlysalary;

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Over and Over Again GEEN163

St. Edmund Preparatory High School Brooklyn, NY

LAB 13: ARRAYS (ONE DIMINSION)

CISC 181 Lab 2 (100 pts) Due: March 4 at midnight (This is a two-week lab)

Topic 11 Scanner object, conditional execution

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Ch. 6. User-Defined Methods

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

JAVA PROGRAMMING (340)

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Lesson 7 Part 2 Flags

Exercise 0.1 (Review from lectures no submission required)

Computação I. Exercises. Leonardo Vanneschi NOVA IMS, Universidade Nova de Lisboa. Leonardo Vanneschi Computação I NOVA IMS

Welcome to the Primitives and Expressions Lab!

Lesson 10: Quiz #1 and Getting User Input (W03D2)

CS141 Programming Assignment #5

CSE 1223: Exam II Autumn 2016

Chapter 8 Multi-Dimensional Arrays

CMPS 11 Introduction to Programming Midterm 1 Review Problems

CSIS 10A Assignment 9 Solutions

King Saud University College of Computer and Information Sciences Computer Science Department

Chapter 5 Lab Methods

Beginning Programming (Java) Test 2/Version 1 CMSC 1513 Spring 2013

Netbeans tutorial:

CEN 414 Java Programming

Final. Your Name CS Fall 2014 December 13, points total Your Instructor and Section

Bjarne Stroustrup. creator of C++

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Practice Problems: Instance methods

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Conditional Execution

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Claremont McKenna College Computer Science

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

2.2 - Making Decisions

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Methods

Week 2 assignment:

CSIS 10A Assignment 4 SOLUTIONS

Transcription:

King Saud University College of Computer & Information Science CSC111 Lab04 Conditional Statements All Sections ------------------------------------------------------------------- Instructions Web-CAT submission URL: http://10.131.240.28:8080/web-cat/webobjects/web- CAT.woa/wa/assignments/eclipse Objectives: 1- Student should learn how to program using selection statements with combined conditions. 2- Student should learn how to combine conditions using logical operators (!, &&, and ) 3- Student should learn how to write expressions using the conditional expression

Lab Exercise 1 How cold is it outside? Temperature by itself is not enough. In 2001, the National Weather Service (NWS) in United States implemented the new wind-chill temperature to measure the coldness using temperature and wind speed. The formula is where is the outside temperature measured in degrees Fahrenheit and is the speed measured in miles per hour. is the wind-chill temperature. The formula cannot be used for wind speeds below 2 mph or temperatures below o F or above 41 o F. Write a program that prompts the user to enter a temperature and a wind speed. The program displays the wind-chill temperature if the input is valid; otherwise, it displays a message indicating whether the temperature and/or wind speed is invalid. Here are sample runs: Enter the temperature in Fahrenheit: 32 Enter the wind speed miles per hour: 30 The wind chill index is 17.59665069469402 Enter the temperature in Fahrenheit: 80 Temperature must be between -58F and 41F

Enter the temperature in Fahrenheit: 20 Enter the wind speed miles per hour: 1 Speed must be greater than or equal to 2 Solution 1- Create a new project in eclipse and name it lab04 2- Create a new class and name it WindChill. Make sure you choose the public static void main option. 3- Write the program as following (you can ignore comments): import java.util.*; public class WindChill { /** * @param args */ public static void main(string[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); System.out.print("Enter the temperature in Fahrenheit:"); double ta = s.nextdouble(); if(ta>=-58 && ta<=41) { System.out.println("Enter the wind speed miles per our:"); double v=s.nextdouble(); if(v>=2) { double twc = 35.74 + 0.6215*ta - 35.75*Math.pow(v, 0.16) + 0.4275*ta*Math.pow(v, 0.16); System.out.println("The wind chill index is "+twc); else System.out.println("Speed must be greater than or equal to 2"); else System.out.println("Temperature must be between -58F and 41F");

4- When you are done, save your program and run it. Make sure it prints the output as shown above. 5- Submit your program to WebCAT through. Ask your TA for help. Lab Exercise 2 The two roots of a quadratic equation using the following formula: can be obtained and is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots. Write a program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display The equation has no real roots. Note that you can use Math.pow(x, 0.5) to compute.

Enter a, b, c: 1 3 1 The equation has two roots -0.3819660112501051 and - 2.618033988749895 Enter a, b, c: 1 2 1 The equation has one root -1.0 Enter a, b, c: 1 2 3 The equation has no real roots Here are some sample runs: Solution 6- Use the same project lab04 that you created before 7- Create a new class and name it QuadEquation. Make sure you choose the public static void main option. 8- Write the program as following (you can ignore comments):

import java.util.scanner; public class QuadEquation { /** * @param args */ public static void main(string[] args) { Scanner s=new Scanner(System.in); System.out.println("enter a, b, c:)"); double a=s.nextdouble(); double b=s.nextdouble(); double c=s.nextdouble(); double disc=b*b-4*a*c; double r1,r2; if(disc<0) System.out.println("the equation has no real roots"); else if(disc==0) { r1=-b/(2*a); System.out.println("The equation has one root "+ r1); else { r1=(-b+math.sqrt(disc))/(2*a); r2=(-b-math.sqrt(disc))/(2*a); System.out.println("The equation has two roots :"+r1+" and "+r2);

9- When you are done, save your program and run it. Make sure it prints the output as shown above. 10- Submit your program to WebCAT through. Ask your TA for help. Lab Exercise 3 Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floatingpoint number. Enter integers ending with 0: 1 2-1 3 0 The number of positives is 3 The number of negatives is 1 The total is 5 The average is 1.25 Here are some sample runs: Enter integers ending with 0: 0 no numbers are entered except 0 Solution 1- Create a new project in eclipse and name it lab04

2- Create a new class and name it CountPosNeg. Make sure you choose the public static void main option. 3- Write the program as following (you can ignore comments): 4- When you are done, save your program and run it. Make sure it prints the output as shown above. 5- Submit your program to WebCAT through. Ask your TA for help.

Lab Exercise 4 Write a program that prompts the user to enter the number of students and each student s name and score (at least two students), and finally displays the student with the highest score and the student with the second-highest score. Here is a sample runs: Enter the number of students: 4 Enter a student name: Mohammed Enter a student score: 75 Enter a student name: Ali Enter a student score: 85 Enter a student name: Fahad Enter a student score: 98 Enter a student name: Khalid Enter a student score: 65 Top two students: Fahad's score is 98.0 Ali's score is 85.0 Solution 11- Use the same project lab04 that you created before 12- Create a new class and name it HighScore. Make sure you choose the public static void main option. 13- Write the program as following (you can ignore comments):

6- When you are done, save your program and run it. Make sure it prints the output as shown above. 7- Submit your program to WebCAT through. Ask your TA for help. Done

Ex-3 Solution import java.util.scanner; public class integers { public static void main(string[] args) { Scanner s=new Scanner(System.in); System.out.println("Enter integers ending with 0:"); int x,pos=0,neg=0,sum=0; do{ x=s.nextint(); sum+=x; if(x>0) pos++; else if(x<0) neg++; while(x!=0); double avg=(double)sum/(pos+neg); System.out.println("pos="+pos+" neg="+neg); System.out.println(avg); System.out.println(sum);