CS141 Programming Assignment #8

Similar documents
CS141 Programming Assignment #6

CS141 Programming Assignment #4

Midterm Examination (MTA)

CS141 Programming Assignment #5

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

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

Introduction to Java Applications

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

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

download instant at

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

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

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

2.8. Decision Making: Equality and Relational Operators

16. Give a detailed algorithm for making a peanut butter and jelly sandwich.

Introduction to Java Applications

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Object Oriented Programming. Java-Lecture 1

Programming with Java

Loops. Repeat after me

Introduction to Computer Science Unit 3. Programs

Building Java Programs

1. A company has vans to transport goods from factory to various shops. These vans fall into two categories: 50 items 150 items

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

Example: Monte Carlo Simulation 1

Chapter 4: Control structures. Repetition

Chapter 3 Lab Decision Structures

Java Programming Language. 0 A history

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

CS111: PROGRAMMING LANGUAGE II

AP COMPUTER SCIENCE A

CS111: PROGRAMMING LANGUAGE II

Chapter 3. Selections

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

Introduction to Computer Science Unit 2. Exercises

Chapter 4: Control structures

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

Arrays OBJECTIVES. In this chapter you will learn:

Task 1: Print a series of random integers between 0 and 99 until the value 77 is printed. Use the Random class to generate random numbers.

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

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

Section 2.2 Your First Program in Java: Printing a Line of Text

AP CS Unit 3: Control Structures Notes

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

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

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

Control Statements: Part 1

Write a program which converts all lowercase letter in a sentence to uppercase.

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

ECE 161 WEEK 4 Introduction to Programing in Java

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

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

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Repetition CSC 121 Fall 2014 Howard Rosenthal

Introduction to Computer Science Unit 2. Notes

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

CSC 1051 Data Structures and Algorithms I

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

ITERATION WEEK 4: EXMAPLES IN CLASS

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

Program 12 - Spring 2018

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

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

1. Find the output of following java program. class MainClass { public static void main (String arg[])

CS141 Programming Assignment #10

Practice Midterm 1. Problem Points Score TOTAL 50

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

CSC 1051 Data Structures and Algorithms I

Practice Midterm 1 Answer Key

CS110: PROGRAMMING LANGUAGE I

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

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

Oct Decision Structures cont d

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

Chapter 8 Multidimensional Arrays

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

Introduction to Computer Science Unit 2. Notes

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

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

1 class Lecture4 { 2 3 "Loops" / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207

CS Week 5. Jim Williams, PhD

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name:

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

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

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Supplementary Test 1

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

CSCI 1101 Winter 2017 Laboratory No Submission deadline is p.m. (5 minutes to midnight) on Saturday, February 4th, 2017.

CS 161. SAMPLE FINAL Fall 2014

CS1150 Principles of Computer Science Arrays

Date: Dr. Essam Halim

COSC 1010 Introduction to Computer Programming

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Lecture 6. Drinking. Nested if. Nested if s reprise. The boolean data type. More complex selection statements: switch. Examples.

Transcription:

CS141 Programming Assignment #8 Due Sunday, April 14th. 1- Write a class with methods to do the following output: a) 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 b) 1 2 3 4 5 4 3 2 1 1 2 3 4 * 4 3 2 1 1 2 3 * * * 3 2 1 1 2 * * * * * 2 1 1 * * * * * * * 1 1 2 * * * * * 2 1 1 2 3 * * * 3 2 1 1 2 3 4 * 4 3 2 1 1 2 3 4 5 4 3 2 1 c) Read odd integer x and the output depended on x for example: x = 5 x=7 * * *** *** ***** ***** *** ******* * ***** *** * //Assignment# 8 Q1 //Due Date: April 14th,2013 //This program will output three different shapes using three different methods. import java.util.scanner; public class Q1 { methoda(); methodb(); System.out.print("Enter an odd integer: "); int num = input.nextint(); methodc(num); /** Method to print the first shape mentioned above **/ public static void methoda (){ for (int i = 5; i >= 1; i--){ for (int j = 5; j > i; j--){ System.out.print( " " ); for (int j = 1; j <= i; j++){ System.out.print(i+" ");

/** Method to print the second shape mentioned above **/ public static void methodb(){ for (int i = 5; i >= 1; i--){ for (int j = 1; j <= i; j++) System.out.print(j+" "); for (int j = i; j < 5; j++) System.out.print("* "); for (int j = i; j < 4; j++) System.out.print("* "); for (int j = i; j >= 1; j--){ if (j == 5) continue; System.out.print(j+" "); for (int i = 2; i <= 5; i++){ for (int j = 1; j <= i; j++) System.out.print(j+" "); for (int j = i; j < 5; j++) System.out.print("* "); for (int j = i; j < 4; j++) System.out.print("* "); for (int j = i; j >= 1; j--){ if (j == 5) continue; System.out.print(j+" "); /** Method to print the third shape mentioned above **/ public static void methodc(int x){ if(x%2!= 0){ for (int i = 1; i <= x; i+=2){ for (int j = 1; j <= (x - i)/2; j++) System.out.print(" "); for (int j = 1; j <= i; j++) System.out.print("*"); for (int i = x-2; i >= 1; i-=2){ for (int j = 1; j <= (x - i)/2; j++) System.out.print(" "); for (int j = 1; j <= i; j++) System.out.print("*"); System.out.printf("Error!! %d is not odd!!", x); // end class

2- Write a program that have a menu of 4 options: Please choose from the menu: 1- Print a number digits. 2- Calculate the sum between two integers. 3- Find all numbers divisible by a number in a given range. 4- Exit Option 1 asks for a positive integer number n and prints its digits. Example: Please enter a positive integer: 6545 6545= 6 5 4 5 Option 2 asks for two integers start and end then it finds the sum of all the integers between start and end. Example: Please enter the start and the end integers: 2 6 The sum for all numbers between 2 and 6 = 12 Option 3 asks for three integers start, end and a number then it finds those integers between start and end that are divisible by the number and prints them. Example: Please enter the start and the end integers: 1 10 Please enter the number you want to check for: 3 The numbers are divisible by 3 is : 3 6 9 Option 4 Exit the program. //Assignment# 8 Q2 //Due Date: April 14th,2013 //This program is Menu driven program options: // 1)print a given number's digits // 2)calculate the sum between two integers // 3)find all numbers divisible by a number in a given range //4)Exit.

import java.util.scanner; public class Q2 { int choice; // stores the user's choice from the menu do{ System.out.println("Please choose from the menu:\n"); System.out.println("1-Print the number's digits."); System.out.println("2-Calculate the sum between two integers."); System.out.println("3-Find all numbers divisible by a number" + " in a given range."); System.out.println("4-Exit."); choice = input.nextint(); switch(choice){ case 1: printdigits(); case 2: printsum(); case 3: printdivisors(); case 4: System.out.println("Bye!!"); default:system.out.println("invalid input. Try again!!"); while (choice!= 4); /** method to print the digits in the number **/ public static void printdigits(){ Scanner input = new Scanner (System.in); int num,numcopy,places=0, factor=1; System.out.println("Please enter a positive integer: "); num = input.nextint(); numcopy= num; while(numcopy >0){ // to count how many digits in the number numcopy/=10; places++; places--; while(places >0){ // build the factor that will divide the num factor*=10; places--; System.out.printf("%d=", num); while(num >0){ //brake num from left to right. numcopy =num % factor ; num/= factor; System.out.printf(" %d", num); factor/=10; num=numcopy;

/** method to print the sum of numbers between a range **/ public static void printsum(){ int start; // stores the starting value of the range int end; // stores the ending value of the range int sum = 0; // stores the sum of the value between the range System.out.println("Please enter the start and the end integers:"); start = input.nextint(); end = input.nextint(); for (int i = start+1; i < end; i++) sum += i; System.out.printf("The sum for all numbers between %d ", start); System.out.printf("and % d = %d\n", end, sum); /** method to find the numbers divisible by a certain number **/ public static void printdivisors(){ Scanner input = new Scanner (System.in); int start; // stores the beginning of the range int end; // stores the end of the range int num; // stores the number to check for divisibility of System.out.println("Please enter the start and end integers:"); start = input.nextint(); end = input.nextint(); System.out.println("Please enter the number you want to check for:"); num = input.nextint(); System.out.printf("The numbers are divisible by %d is:\n", num); for (int i = start+1; i < end; i++) if (i % num == 0) System.out.printf("%d ", i); //end class 3- Write a class that reads 10 integers. The program should calculate and print the sum of even and odd numbers. You program should have three methods: - Read method that will be responsible for reading the numbers. - IsOdd a Boolean function that checks if a number is odd or not. - IsEven a Boolean function that checks if a number is even or not. Example: Please enter 10 integers: 1 2 3 4 5 6 7 8 9 10 The sum of odd elements is: 25 The sum of even elements is: 30 //Assignment# 8 Q3 //Due Date: April 14th,2013 //This program will find the sum of the even and the sum of the odd numbers.

import java.util.scanner; public class Q4 { private static int sumeven; //stores the sum of the even numbers private static int sumodd; //stores the sum of the odd numbers Read(); System.out.printf("The sum of odd elements is: %d\n",sumodd); System.out.printf("The sum of even elements is: %d\n",sumeven); /** Method to read 10 numbers and compute the sum of the even ** ** and the sum of the odd numbers **/ public static void Read(){ sumeven = sumodd = 0; System.out.println("Please enter 10 integers: "); for (int i = 1; i <= 10; i++){ int num = input.nextint(); //stores number the user input if(isodd(num)) sumodd+=num; if(iseven(num)) sumeven+=num; /** Method to check if value is odd **/ public static boolean IsOdd(int x){ if (x%2 == 0) return false; return true; /** Method to check if value is even **/ public static boolean IsEven(int x){ if (x%2 == 0) return true; return false; //end class 4- Write a class that reads 8 integers and finds the largest and the smallest numbers. The main should print the large and small numbers. Example: Please enter 8 integers: 5-6 3 4 3 11 0 8 The largest number is: 11 The smallest number is: -6 //Assignment# 8 Q4

//Due Date: April 14th,2013 //This program will read 8 numbers and print the largest and smallest of them. import java.util.scanner; public class Q4 { private static int large; private static int small; Read(); System.out.printf("The largest number is: %d\n", large); System.out.printf("The smallest number is: %d\n", small); //end main /** Method to read values and find largest and smallest values **/ public static void Read(){ System.out.println("Please enter 8 integers:"); int num = input.nextint(); large = small = num; for (int i = 2; i <=8; i++){ num = input.nextint(); large = Math.max(large, num); small = Math.min(small, num); //end class 5- Write a class that read a birthday as three integers day, month and year. It passes these three integers to a method that finds the age. Assume that we checking for the age until 1/1/2013. Example: Please enter your Birthday as 3 numbers DD MM YYYY: 01 01 1950 Your age is: 63 years 0 months and 0 days //Assignment# 8 Q5 //Due Date: April 14th,2013 //This program will read a birthday and finds the age until 1/1/2013. import java.util.scanner; public class Q5 { private static int years; // stores the number of years private static int months;// stores the number of months private static int days; // stores the number of days readage(); System.out.println("Your age is:"); System.out.printf("%d years %d months and %d days\n", years, months, days);

/** Method to read the day, month and year from the user then **/ /** call another method to compute the age **/ public static void readage() { int yyyy; // stores the birth year int mm; // stores the birth month int dd; // stores the birth day boolean ok;// stores flag for the loop System.out.println("Please enter your Birthday as 3 numbers DD MM YYYY:"); do { dd = input.nextint(); mm = input.nextint(); yyyy = input.nextint(); ok = false; if (yyyy < 0) // check if year is valid System.out.println("Years cannot be negative!!!"); if (mm > 12 mm < 0) // check if month is valid System.out.println("Months should be between 1 and 12!!!"); if ((dd < 0 dd > 31) (dd > 30 && (mm == 4 mm == 6 mm ==9 mm == 11)) (dd > 28 && mm == 2 && yyyy % 4!= 0) (dd > 29 && mm == 2)) // check if day is valid System.out.println("day does not exist in the given month!!!"); if (yyyy > 2012 ) //check day is not after given "until" date System.out.println("Dates must be before 01/01/2013!!!"); ok = true; if (!ok) // if invalid date System.out.println("Please enter the date again (DD MM YYYY):"); while (!ok); findage(dd, mm, yyyy); // end readage /** Method to compute the age **/ public static void findage(int d, int m, int y) { years = 2012 - y; months = 12 - m; if (d == 1) { days = 0; months++; switch (m) { case 4: case 6: case 9:case 11: days = 31 - d; case 2: if (y % 4 == 0) days = 30 - d; days = 29 - d; default: days = 32 - d;

if ((years==0 )&& (months ==12) && (days ==0 )) //special case { years= 1; months=0; days =0 ; // end findage // end class