(Eng. Hayam Reda Seireg) Sheet Java 1. Write a program to compute the area and circumference of a rectangle 3 inche wide by 5 inches long. What changes must be made to the program so it works for a rectangle 6.8 inches wide by 2.3 inches long? public class AreaOfRectangle public static void main(string[] args) int width=3,height=5; // double width=6.8,height=2.3; int area,circumference; // double area,circumference; area = width height; circumference = 2 ( width + height); System.out.println("The area of rectangle is "+ area+" the circumference is "+ circumference); 2- Write a program to print your name, and date of birth? public class YourName public static void main(string[] args) System.out.println("My name is kamal and the date of my birth is 4/9/1984"); 3- Write a program to sum up all the numbers in array? package sum.of.the.array; @author hayam public class SumOfTheArray 1
public static void main(string[] args) int size,sum=0; System.out.println("Enter the size of the array "); size=scan.nextint(); int [] x = new int [size]; for( int i=0;i<size;i++) System.out.println("Enter the array "); x[i] =scan.nextint(); sum = sum +x[i]; System.out.println("The sum of the array is "+ sum); 4- Write a program to multiply two arrays? package multiply.array; @author hayam public class MultiplyArray public static void main(string[] args) int size,j=0; System.out.println("Enter the size of the array "); size=scan.nextint(); int [] x = new int [ size ]; int [] m = new int [size]; int [] mul = new int [size]; for( int i=0;i<size;i++) 2
System.out.println("Enter the array 1"); x[i] =scan.nextint(); System.out.println("Enter the array 2"); m[j] =scan.nextint(); mul[i] = x[i] m[j]; for( int i=0;i<size;i++) System.out.println("The result is "+ mul[i] ); 5- Write a program that converts Celsius to Fahrenheit. package celuus; @author hayam public class Celuus public static void main(string[] args) double c,f; System.out.println("Enter the celsius"); c = scan.nextdouble(); f = (9.0/5.0 c+32); System.out.println("the fahrenheit is " + f ); 3
6- Write a program to calculate the volume of a sphere, package volume.of.the.sphere; @author hayam public class VolumeOfTheSphere public static void main(string[] args) double radius,volume; System.out.println("Enter the number of the radius "); radius=scan.nextdouble(); volume =((4.0/3.0) Math.PI Math.pow(radius, 3)); System.out.println("volume of the sphere = " + volume ); 7- Write a program to print out the perimeter of a rectangle given its height and width. perimeter = 2 (width+height) package perimeter; @author hayam public class PERIMETER 4
public static void main(string[] args) // TO CALCULATE THE PERIMETR OF RECTANGLE double width,height,perimeter; width =2.0; height = 3.4; perimeter = 2 (width + height); System.out.println("perimeter =" + perimeter); 8- Write a program that converts kilometers per hour to miles per hour. miles = (kilometers - 0.6213712) package kilometers.per.hours; @author hayam public class KilometersPerHours public static void main(string[] args) 5
double kilometers,miles; System.out.println("Enter the value of the kilometers per hours"); kilometers = scan.nextdouble(); miles= (kilometers - 0.6213712 ); System.out.println("miles per hours"+ miles); 9- Write a program that takes hours and minutes as input and outputs the total number of minutes ( 1 hour 30 minutes = 90 minutes). package hours.and.minutes; @author hayam public class HoursAndMinutes public static void main(string[] args) int hours,minutes,a=0; 6
System.out.println("Enter the hours and minutes"); hours = scan.nextint(); minutes = scan.nextint(); if (minutes>=0 && hours>=0&& hours <= 12 && minutes<=60) for (int i=1;i<=hours;i++) a = a + 60; minutes = minutes + a; System.out.println(minutes + " minutes"); else System.out.println("Error input"); 10- Write a program that takes an integer as the number of minutes and outputs the total hours and minutes (90 minutes = 1 hour 30 minutes). package minutes; 7
@author hayam public class MINUTES public static void main(string[] args) int minutes, hours=0,a=60; System.out.println("Enter the minutes "); minutes = scan.nextint(); if (minutes>60 && minutes>=0) for (int i=60;i<=minutes;i=+60) hours = hours + 1; minutes = minutes - a; System.out.println(hours +"hours " + minutes+ " minutes"); else System.out.println(minutes + " minutes"); 8
11- Write a program to find the square of the distance between two points. package distance; @author hayam public class Distance public static void main(string[] args) double p1,p2,q1,q2,distance; System.out.println("Enter the x1 for the first point "); p1 =scan.nextint(); System.out.println("Enter the y1 for the first point "); p2 =scan.nextint(); System.out.println("Enter the x2 for the second point "); q1 =scan.nextint(); System.out.println("Enter the y2 for the second point "); q2 =scan.nextint(); //if p = (p1, p2) and q = (q1, q2) //standard Euclidean distance can be squared in order to place //progressively greater weight on objects that are further apart. In this case, the equation //d(p,q) = (p1 q1)2 + (p2 q2)2 +... + (pi qi)2 +... + (pn qn)2. //the program calculate the square between distance between two points distance = (Math.pow((p1-q1),2)+ Math.pow((p2-q2),2)); System.out.println("standard Euclidean distance can be squared is " + distance); 9
12- A professor generates letter grades using the following table: Given a numeric grade, print the letter. package degree; @author hayam public class Degree public static void main(string[] args) System.out.println("Enter the numerica grade "); System.out.println("Please don't enter number greater than 100"); 10
int x = scan.nextint(); if(x>100) System.out.println("bad input"); else if ( x>=91 x==100) System.out.println("A"); else if ( x>=81 x==90) System.out.println("B"); else if (x>=71 x==80) System.out.println("C"); else if (x>=61 x==70) System.out.println("D"); else if (x>=0 x==60) System.out.println("F"); 13- Modify the previous program to print out a + or - after the letter grade based on the last digit of the score. The modifiers are listed in the following table For example, 81=B-, 94=A, and 68=D+. Note: n F is only an F. There is no F+ or F-. package grades; 11
@author hayam public class Grades public static void main(string[] args) System.out.println("Enter the numeric grades "); int x = scan.nextint(); String astring = Integer.toString(x); if (x>100) System.out.println("error,wrong input"); else if (x >=91 x==100 ) if (astring.charat(1)==56 astring.charat(1)==48 astring.charat(1)==57)// NUMBER 8 IN THE ASCI CODE IS 56 AND NUMBER 0 IN THE ASCI CODE 48 AND NUMBER 9 IN THE ASCI CODE IS 57 System.out.println("A+"); else if (astring.charat(1)==49 astring.charat(1)==51 astring.charat(1)==50) System.out.println("A-"); 12
else System.out.println("A"); else if (x>=81 x==90) if (astring.charat(1)==56 astring.charat(1)==48 astring.charat(1)==57) System.out.println("B+"); if (astring.charat(1)==49 astring.charat(1)==51 astring.charat(1)==50) System.out.println("B-"); else System.out.print("B "); else if (x>=71 x==80) if (astring.charat(1)==56 astring.charat(1)==48 astring.charat(1)==57) System.out.println("C+"); else if (astring.charat(1)==49 astring.charat(1)==51 astring.charat(1)==50) System.out.println("C-"); else System.out.println("C"); else if (x>=61 x==70) if (astring.charat(1)==56 astring.charat(1)==48 astring.charat(1)==57) 13
System.out.println("D+"); else if (astring.charat(1)==49 astring.charat(1)==51 astring.charat(1)==50) System.out.println("D-"); else System.out.println("D"); else if (x>=0 x==60) System.out.println("F"); 14- Write a program that takes a series of numbers and counts the number of positive and negative values. package count.positive; @author hayam public class CountPositive public static void main(string[] args) 14
int size, count=0,n=0; System.out.println("Enter the size of the array "); size= scan.nextint(); int [] x = new int [size]; for( int i=0;i<size;i++) System.out.println("Enter the array "); x[i] =scan.nextint(); if ( x[i]> 0) System.out.println("The positive value is " + ++ count); else if (x[i] <0) System.out.println("The negative value is " + ++ n); else if (x[i]==0) System.out.println("The value is zero "); 15- Write a program to average n numbers. package average.number; 15
@author hayam public class AVERAGENUMBER public static void main(string[] args) int size; double sum=0,n; double average; System.out.println("Enter the size of the array "); size=scan.nextint(); int [] x = new int [size]; for( int i=0;i < size;i++) System.out.println("Enter the array "); x[i] =scan.nextint(); sum = sum + x[i]; average = sum / size; 16
System.out.println(" the average value is "+ average) ; 17- Write a program that converts numbers to words. Example: 895 results in ''eight nine five." package opdf; @author hayam public class OPDF public static void main(string[] args) String in; String [] name= "zero","one","two","three","four","five","six","seven","eight","nine"; System.out.println("Enter the number "); in=scan.next(); for(int i=0;i<in.length();i++) 17
if (in.charat(i)>=48 && in.charat(i)<59) System.out.print(name[(in.charAt(i)-48)]+" "); 16- Write a program that reads a character and prints out whether or not it is a vowel or a consonant. package consonant; @author hayam public class Consonant public static void main(string[] args) 18
System.out.println("Enter the character"); String x =scan.next(); x= x.touppercase(); if( x.charat(0) =='A' x.charat(0)=='e' x.charat(0)=='i' x.charat(0)== 'O' x.charat(0)=='u' ) System.out.println("vowels"); else System.out.println("cononsant"); 19
1. Write a program that declares and initializes an array a[] of size 1000 and accesses a[1000]. Does your program compile? What happens when you run it? int [ ] a = new int [1000]; for(int i=0;i<1000;i++) a[i]=i; int x=a[1000];it is wrong because the program cannot access 1000 so it can access a[999], the program can access from 0 to 999 when you initialized the size of array 1000. 2. Given two vectors of length N that are represented with one-dimensional arrays, write a code fragment that computes the Euclidean distance between them (the square root of the sums of the squares of the differences between corresponding entries). package vector; @author hayam public class Vector public static void main(string[] args) int size; double out,sum=0; 20
System.out.println("Enter the size of the array 1"); size=scan.nextint(); int A[]= new int[size]; int B[]= new int[size]; int C[]= new int[size]; int D[]= new int[size]; for(int i=0;i<size;i++) System.out.println("Enter the array of x1"); A[i]=scan.nextInt(); System.out.println("Enter the array of y1"); B[i]=scan.nextInt(); System.out.println("Enter the array of x2"); C[i]=scan.nextInt(); System.out.println("Enter the array of y2"); D[i]=scan.nextInt(); for(int i=0;i<size;i++) sum=(math.pow(c[i]-a[i],2)+math.pow(d[i]-b[i],2)); System.out.println("the result is "+ sum ); out = Math.sqrt(sum); System.out.println("the result is "+ out); 21
3. Write a code fragment that reverses the order of a one-dimensional array a[] of double values. Do not create another array to hold the result. Hint: Use the code in the text for exchanging two elements. Solution. int N = a.length; for (int i = 0; i < N/2; i++) double temp = a[n-i-1]; a[n-i-1] = a[i]; a[i] = temp; 4. What is wrong with the following code fragment? int[] a; for (int i = 0; i < 10; i++) a[i] = i i; Solution: It does not allocate memory for a[] with new. The code results in a variable might not have been initialized compile-time error. 5. What does the following code fragment print? int [] a = new int[10]; for (int i = 0; i < 10; i++) a[i] = 9 - i; for (int i = 0; i < 10; i++) a[i] = a[a[i]]; for (int i = 0; i < 10; i++) System.out.println(a[i]); Solution is 0 1 2 3 4 4 3 2 1 0 22
6. What values does the following code put in the array a? N = 10; int[] a = new int[n]; a[0] = 0; a[1] = 1; for (int i = 0; i < N; i++) a[i] = a[i-1] + a[i-2]; System.out.println(a[i]); Solution : Error, it is out of range of the index (Array Index Out Of Bounds Exception). 7. What does the following code fragment print? int[] a = 1, 2, 3 ; int[] b = 1, 2, 3 ; System.out.println(a == b); Solution: It prints false. The == operator compares whether the (memory addresses of the) two arrays are identical, not whether their corresponding values are equal. 8. Write a program that computes the product of two square matrices of boolean values, using the or operation instead of + and the and operation instead of. package or.and.matrix; @author hayam public class OrAndMatrix public static void main(string[] args) Scanner scan = new Scanner(System.in); int i, n, j, k, w,o,f; 23
System.out.println("Enter the row of the first matrix"); i = scan.nextint(); System.out.println("Enter the column of the first matrix"); j = scan.nextint(); System.out.println("Enter the row of the second matrix"); k = scan.nextint(); System.out.println("Enter the column of the second matrix"); n = scan.nextint(); int[][] z = new int[i][n]; int[][] x = new int[i][j]; int[][] y = new int[k][n]; if (j == k) for ( o = 0; o < i; o++) for ( f = 0; f < j; f++) System.out.println("Enter the value of the first matrix " + "x[" + o + "][" + f + "]"); x[o][f] = scan.nextint(); for ( o = 0; o < k; o++) for ( f = 0; f < n; f++) System.out.println("Enter the value of the secopnd matrix " + "y[" + o + "][" + f + "]"); y[o][f] = scan.nextint(); 24
else System.out.println("Error input"); for (int b = 0; b < i; b++) for (int c = 0; c < n; c++) int sum; if (j >= 3 k >= 3) w = j = k; for (int d = 2; d < w; d++) sum = x[b][d] & y[d][c]; z[b][c] = x[b][0] & y[0][c] x[b][1] & y[1][c] sum; System.out.println("z" + "[" + b + "][" + c + "]" + " = " + z[b][c]); else z[b][c] = x[b][0] & y[0][c] x[b][1] & y[1][c]; System.out.println("z" + "[" + b + "][" + c + "]" + " = " + z[b][c]); 25
9. Write a code fragment to multiply two rectangular matrices that are not necessarily square. Note: For the dot product to be well-defined, the number of columns in the first matrix must be equal to the number of rows in the second matrix. Print an error message if the dimensions do not satisfy this condition. package project; @author hayam public class Project public static void main(string[] args) int i,n,j,k,w; System.out.println("Enter the row of the first matrix"); i = scan.nextint(); System.out.println("Enter the column of the first matrix"); j = scan.nextint(); System.out.println("Enter the row of the second matrix"); 26
k = scan.nextint(); System.out.println("Enter the column of the second matrix"); n = scan.nextint(); int [][] z = new int [i][n]; int [][] x = new int [i][j]; int [][] y = new int [k][n]; if (j==k) for(int o=0;o<i;o++) for(int f=0;f<j;f++) System.out.println("Enter the value of the first matrix "+ "x["+o+"]["+f+"]"); x[o][f]= scan.nextint(); for(int o=0;o<k;o++) for(int f=0;f<n;f++) System.out.println("Enter the value of the secopnd matrix " + "y["+o+"]["+f+"]"); y[o][f]= scan.nextint(); for(int b=0;b<i;b++) 27
for(int c=0;c<n;c++) int sum; if (j>=3 k>=3) w=j=k; for(int d=2;d<w;d++) sum=x[b][d]y[d][c]; z[b][c]= x[b][0]y[0][c]+x[b][1]y[1][c]+sum; System.out.println("z"+"["+b+"]["+c+"]"+" = " + z[b][c]); else z[b][c]= x[b][0]y[0][c]+x[b][1]y[1][c]; System.out.println("z"+"["+b+"]["+c+"]"+" = "+ z[b][c]); else System.out.println("Error input"); 28