(Eng. Hayam Reda Seireg) Sheet Java



Similar documents
Sample CSE8A midterm Multiple Choice (circle one)

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Part I. Multiple Choice Questions (2 points each):

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

JAVA ARRAY EXAMPLE PDF

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Introduction to Java. CS 3: Computer Programming in Java

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

J a v a Quiz (Unit 3, Test 0 Practice)

The following program is aiming to extract from a simple text file an analysis of the content such as:

Topic 11 Scanner object, conditional execution

Arrays in Java. Working with Arrays

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

CS114: Introduction to Java

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Introduction to Java

CS170 Lab 11 Abstract Data Types & Objects

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Example of a Java program

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Basics of Java Programming Input and the Scanner class

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Explain the relationship between a class and an object. Which is general and which is specific?

Building Java Programs

How do you compare numbers? On a number line, larger numbers are to the right and smaller numbers are to the left.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Interactive Applications (CLI) and Math

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

CS 121 Intro to Programming:Java - Lecture 11 Announcements

LOOPS CHAPTER CHAPTER GOALS

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

AP Computer Science Java Mr. Clausen Program 9A, 9B

Java Basics: Data Types, Variables, and Loops

Perimeter, Area, and Volume

Building Java Programs

APPLICATIONS AND MODELING WITH QUADRATIC EQUATIONS

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

In this Chapter you ll learn:

Chapter 3. Input and output. 3.1 The System class

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Programmierpraktikum

Lab 5: Bank Account. Defining objects & classes

Chapter 2 Introduction to Java programming

Using Two-Dimensional Arrays

13 File Output and Input

Area of a triangle: The area of a triangle can be found with the following formula: in

Compiler Construction

AP Computer Science Java Subset

Building Java Programs

Iteration CHAPTER 6. Topic Summary

Unit 6. Loop statements

Homework/Program #5 Solutions

Building Java Programs

AP Computer Science Static Methods, Strings, User Input

Data Structures using OOP C++ Lecture 1

Quick Reference ebook

Characteristics of the Four Main Geometrical Figures

Chapter 2: Elements of Java

Object-Oriented Programming in Java

Chapter 2 Elementary Programming

Object Oriented Software Design

Moving from CS 61A Scheme to CS 61B Java

Arrays. Introduction. Chapter 7

5 Arrays and Pointers

Solutions to Homework 6

FACTORING OUT COMMON FACTORS

Continuous Integration Part 2

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Basic Math for the Small Public Water Systems Operator

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

F ahrenheit = 9 Celsius + 32

Keystone National High School Placement Exam Math Level 1. Find the seventh term in the following sequence: 2, 6, 18, 54

Statements and Control Flow

Line-based file processing

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

AP Computer Science File Input with Scanner

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Programming Fundamentals I CS 110, Central Washington University. November 2015

An Incomplete C++ Primer. University of Wyoming MA 5310

Calculating Area, Perimeter and Volume

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

Using Files as Input/Output in Java 5.0 Applications

Install Java Development Kit (JDK) 1.8

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Introduction to Programming

More on Objects and Classes

Transcription:

(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