1
(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 { /**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here
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; import java.util.Scanner; /** * * @author hayam */
public class SumOfTheArray { /**
2 */
public static void main(String[] args) { // TODO code application logic here int size,sum=0;
Scanner scan = new Scanner (System.in); 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; import java.util.Scanner; /** * * @author hayam */
public class MultiplyArray { /**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here int size,j=0;
Scanner scan = new Scanner (System.in); 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++) {
3 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; import java.util.Scanner; /** * * @author hayam */
public class Celuus { /**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here double c,f;
Scanner scan = new Scanner (System.in); System.out.println("Enter the celsius"); c = scan.nextDouble(); f = (9.0/5.0 * c+32); System.out.println("the fahrenheit is " + f ); } }
4 6- Write a program to calculate the volume of a sphere,
package volume.of.the.sphere; import java.util.Scanner; /** * * @author hayam */
public class VolumeOfTheSphere { /**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here
double radius,volume;
Scanner scan = new Scanner (System.in);
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 */
5 /**
* @param args the command line arguments */
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; import java.util.Scanner; /** * * @author hayam */
public class KilometersPerHours { /**
* @param args the command line arguments */
6 // TODO code application logic here
double kilometers,miles;
Scanner scan = new Scanner (System.in);
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; import java.util.Scanner; /** * * @author hayam */
public class HoursAndMinutes {
/**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here int hours,minutes,a=0;
7
Scanner scan = new Scanner (System.in);
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; import java.util.Scanner; /**
8 * @author hayam
*/
public class MINUTES {
/**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here int minutes, hours=0,a=60;
System.out.println("Enter the minutes "); Scanner scan = new Scanner (System.in); 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 {
9 }
} }
11- Write a program to find the square of the distance between two points.
package distance; import java.util.Scanner; /** * * @author hayam */
public class Distance { /**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here double p1,p2,q1,q2,distance;
Scanner scan = new Scanner (System.in);
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);
} }
10
12- A professor generates letter grades using the following table:
Given a numeric grade, print the letter. package degree; import java.util.Scanner; /** * * @author hayam */
public class Degree {
/**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here
Scanner scan = new Scanner (System.in);
System.out.println("Enter the numerica grade ");
11 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;
12 /**
*
* @author hayam */
public class Grades {
/**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here
System.out.println("Enter the numeric grades "); Scanner scan = new Scanner (System.in);
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-");
13 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) {
14 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; import java.util.Scanner; /** * * @author hayam */
public class CountPositive {
/**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here
15 int size, count=0,n=0;
Scanner scan = new Scanner (System.in);
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; import java.util.Scanner;
16 /**
*
* @author hayam */
public class AVERAGENUMBER { /**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here int size;
double sum=0,n; double average;
Scanner scan = new Scanner (System.in); 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]; }
17 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; import java.util.Scanner; /** * * @author hayam */
public class OPDF { /**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here String in;
String [] name= { "zero","one","two","three","four","five","six","seven","eight","nine"}; Scanner scan = new Scanner (System.in);
System.out.println("Enter the number "); in=scan.next();
for(int i=0;i<in.length();i++) {
18 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; import java.util.Scanner; /** * * @author hayam */
public class Consonant {
/**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here
Scanner scan = new Scanner (System.in);
19 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"); } } }
20
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; import java.util.Scanner; /** * * @author hayam */
public class Vector {
/**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here int size;
double out,sum=0;
21 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); }
22 }
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
23 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; import java.util.Scanner; /** * * @author hayam */
public class OrAndMatrix {
/**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here Scanner scan = new Scanner(System.in); int i, n, j, k, w,o,f;
24 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();
25 } } 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]); }
} }
26 }
}
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; import java.util.Scanner; /** * * @author hayam */
public class Project {
/**
* @param args the command line arguments */
public static void main(String[] args) { // TODO code application logic here
Scanner scan = new Scanner (System.in); 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();
27 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++) {
28 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"); } } }