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