CS141 Programming Assignment #5

Similar documents
System.out.printf("Please, enter the value of the base : \n"); base =input.nextint();

CS141 Programming Assignment #6

Midterm Examination (MTA)

CS141 Programming Assignment #8

CS141 Programming Assignment #4

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Programming with Java

Fundamentals of Programming Data Types & Methods

Example: Monte Carlo Simulation 1

CS141 Programming Assignment #10

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

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Object Oriented Programming. Java-Lecture 6 - Arrays

Object Oriented Programming. Java-Lecture 1

CS212 Midterm. 1. Read the following code fragments and answer the questions.

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

Introduction to Computer Science Unit 2. Notes

University of Palestine. Mid Exam Total Grade: 100

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

ITERATION WEEK 4: EXMAPLES IN CLASS

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

Introduction to Computer Science Unit 2. Notes

1 Short Answer (10 Points Each)

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

Java Classes: Math, Integer A C S L E C T U R E 8

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

CS111: PROGRAMMING LANGUAGE II

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

1 Short Answer (15 Points Each)

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

CS Computers & Programming I Review_01 Dr. H. Assadipour

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

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

CAT.woa/wa/assignments/eclipse

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

Exercise (Revisited)

Arrays. Eng. Mohammed Abdualal

AP CS Unit 3: Control Structures Notes

CSCI 1103: File I/O, Scanner, PrintWriter

Tutorial 03. Exercise 1: CSC111 Computer Programming I

CS 101 Spring 2007 Midterm 2 Name: ID:

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

Reading Input from Text File

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

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

H212 Introduction to Software Systems Honors

Chapter 4: Control Structures I

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

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

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

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

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

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

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

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

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

Tutorial about Arrays

COE 212 Engineering Programming. Welcome to the Final Exam Thursday December 15, 2016

Chapter 3. Selections

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

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

ESC101 : Fundamental of Computing

Supplementary Test 1

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

Loops. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG

Oct Decision Structures cont d

1 import java.util.*; 2 3 // implementing hash tables as an array of linked lists 4 // and using it to check whether two sequencs are permutations of

Object-Oriented Programming in Java

CS 211: Methods, Memory, Equality

JAVA Ch. 4. Variables and Constants Lawrenceville Press

CEN 414 Java Programming

CSCI 1103: File I/O, Scanner, PrintWriter

Computer programming Code exercises [1D Array]

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

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

Variable initialization and assignment

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya, slide 1

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

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

Lecture 8 " INPUT " Instructor: Craig Duckett

Date: Dr. Essam Halim

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp )

Chapter 8 Multidimensional Arrays

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

Arithmetic Compound Assignment Operators

Nested Loops. A loop can be nested inside another loop.

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

COMP6700/2140 Std. Lib., I/O

Topic 11 Scanner object, conditional execution

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

Loops. CSE 114, Computer Science 1 Stony Brook University

Transcription:

CS141 Programming Assignment #5 Due Wednesday, Nov 16th. 1) Write a class that asks the user for the day number (0 to 6) and prints the day name (Saturday to Friday) using switch statement. Solution 1: * assignment 5 Q1 public class Q1 { public static void main(string arg[]){ Scanner input = new Scanner(System.in); int daynumber; System.out.printf("Please, Enter the day number: \n"); daynumber = input.nextint(); switch (daynumber) { case 0: System.out.printf("saturday"); case 1: System.out.printf("sunday"); case 2: System.out.printf("monday"); case 3: System.out.printf("tuesday"); case 4: System.out.printf(" wednesday"); case 5: System.out.printf("thursday"); case 6: System.out.printf("friday"); default: System.out.printf("Invalid value"); //end of switch //end of main

2) Write a class that accepts user input from the console using switch. The class should take a number and then test for the following age ranges: 0 to 10, 11 to 20, 21 to 30, 30 and over. Display a message in the Output window in the following format: user_age + " is between 21 and 30" So if the user enters 27 as the age, the Output window should be this: 27 is between 21 and 30 If the user is 30 or over, you can just display the following message: You are 30 or over Solution 2: * assignment 5 Q2 public class Q2 { public static void main(string[]args){ Scanner input = new Scanner (System.in); int age; System.out.println("please enter your age : "); age =input.nextint(); switch ((age-1)/10) { case 0: System.out.println(age +" is between 0 to 10 "); case 1: System.out.println(age +" is between 11 to 20 "); case 2: System.out.println(age +" is between 21 to 30 "); default : System.out.print("You are 30 or over"); 3) Write a class that reads a string that is guaranteed to contain or. The program should construct another string with or replaced by and. At end, the new string should be displayed on screen. Example: I will study Math or Biology I will study Math and Biology Solution 3:

* assignment 5 Q3 import java.util.scanner ; public class Q3 { Scanner input= new Scanner(System.in); System.out.printf("Please, Enter the text: \n"); String str1 = input.nextline(); String str2=str1.replace("or","and"); System.out.println(str2); //end of main 4) Write a class that will read a string that is guaranteed to have 2 occurrences of the characters er. The program should print the positions of these 2 occurrences. Here is an execution sample: (The input is underlined) Enter your sentence (it should contain 2 occurrences of er ): Ali is older than ahmed and taller also The first occurrence of er is in position 10 The second occurrence of er is in position 32 Solution 4: * assignment 5 Q4 import java.util.scanner ; public class Q4 { Scanner input= new Scanner(System.in); System.out.println("Enter your sentence (it should contain 2 occurrences of er ):"); String str1 = input.nextline(); int pos1 = str1.indexof("er"); int pos2 = str1.indexof("er",pos1 + 2); System.out.printf("The first occurrence of er is in position %d \n",pos1); System.out.printf("The second occurrence of er is in position %d \n",pos2); //end of main

5) Write a class to calculate the value of base exponent For example, if the user base is 3 and the user exponent is 4 then calculates and print 34 (or 3 * 3 * 3 * 3). Assume that exponent is a positive, nonzero integer and that base is an integer. Use for or while statement to control the calculation. Do not use any Math class methods. Solution 5: * assignment 5 Q5 public class Q5 { int base, exp, result = 1 ; Scanner input = new Scanner(System.in); System.out.printf("Please, enter the value of the base : \n"); base =input.nextint(); System.out.printf("Please, enter the value of the exponent : \n"); exp = input.nextint(); for (int i = 0 ; i<exp ; i++ ) result*=base ; System.out.printf("%d to the power %d = %d",base,exp,result); //end of main 6) Write a class that calculates the hypotenuse of a right triangle when the lengths of the other two sides are given. The class should take two arguments of type double and print the hypotenuse as a double. Solution 6: * assignment 5 Q6 public class Q6 { Scanner input = new Scanner(System.in); double side1, side2, hyp, hypsqr; System.out.print("Enter the value for Side 1: "); side1 = input.nextdouble();

System.out.print("Enter the value for Side 2: "); side2 = input.nextdouble(); hypsqr = Math.pow(side1, 2) + Math.pow(side2, 2) ; hyp = Math.sqrt(hypsqr); System.out.printf("The length of the hypotenuse is: %.2f",hyp); //end of main 7) Write a class to do the following : a) prints the maximum integer in the array. b) prints the minimum integer in the array. c) prints the average of integers in the array. d) print the array sorted in ASD and DES order. e) print if array A and B are identical otherwise false f) print the index of the first negative found or -1 Test your class you implemented with the following arrays int [] a = {10, 5, -6, 0, 15, -12, 20; int [] b = {10, 5, -6, 0, 15, -12; Solution 7: * assignment 5 Q7 public class Q7 { int a[] = {10,5,-6,0,15,-12,20 ; int b[] = {10,5,-6,0,15,-12 ; System.out.println("Array a={10,5,-6,0,15,-12,20 \narray b={10,5, -6,0,15,-12 "); /*find the min, max and avg of array a int min =0, max = 0, index = -1 ; float sum = 0 ; for(int i = 0 ;i<a.length ; i++){ if (i ==0 ) min = max = a[0] ; if(a[i]<min) min = a[i];

if(a[i]>max) max = a[i]; sum+=a[i]; if(a[i] < 0 && index ==-1) index = i ; //end of for System.out.printf("\nFor array a the min is : %d, the max is : %d and the average is : %f \n",min,max,sum/a.length); if (index>=0) System.out.printf("\nThe index of the first negative number in array a is %d \n",index); else System.out.println( index); /* compare the arrays if (a.length!= b.length ) System.out.printf("\nThe two arrays a & b are not equal \n"); else { boolean equal=true; for (int i = 0; i < a.length; i++) if (a[i]!=b[i]) equal=false; if (equal) System.out.println("\nThe two arrays are equal "); else System.out.println("\nThe two arrays are not equal "); /* ASD ORDER of array a int t; for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] > a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; //end if //end for //end for System.out.println("Array a in ASD Order:"); for (int i = 0; i <a.length ; i++) System.out.printf("%d ",a[i]); System.out.print("\n\n");

/* DSD ORDER of array a for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] < a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; //end of if //end of j for //end of i for System.out.printf("Array a in DSD Order: \n"); for (int i = 0; i <a.length ; i++) System.out.printf("%d ",a[i]); //end of main