Departments: CS Course:Programming (CS110DT) Class: Level 1
KINGDOM OF SAUDI ARABIA PRINCESS NORA UNIVERSITY FACULTY OF COMPUTER AND
INFORMATION SCIENCES
Lecture 2 – Tutorial What is the output of the following programs? public class Exercise { public static void main(String[] args) { System.out.print("\t"); System.out.print("J"); System.out.print("a"); System.out.println("va"+"! "+ 1+1+0); System.out.println("************"); } }
java! 110 ************
public class Exercise { public static void main(String[] args) { System.out.print("The Expression :\n"+"3+2/25+(9+1)= "+(3+2/25+(9+1))+"\n"); }}
The Expression : 3+2/25+(9+1)= 0
Choose the best answer. 1)-Use the following class definition to answer the questions below. public class Questions1_4 { public static void main(String[ ] args) { System.out.print("Here"); System.out.println("There " + "Everywhere"); System.out.println("But not" + "in Texas");}} 1) The program will print the word "Here" and then print A) "There Everywhere" on the line after "Here" B) "There" on the line after "Here" and "Everywhere" on the line after "There" C) "There Everywhere" on the same line as "Here" D) "ThereEverywhere" on the same line as "Here" E) "ThereEverywhere" on the line after "Here" 1|Page
Prepared by Mrs.Enas
Departments: CS Course:Programming (CS110DT) Class: Level 1
KINGDOM OF SAUDI ARABIA PRINCESS NORA UNIVERSITY FACULTY OF COMPUTER AND
INFORMATION SCIENCES
2) If you want to output the text "hi there", including the quote marks, which of the following could do that? A) System.out.println("hi there"); B) System.out.println(""hi there""); C) System.out.println("\"hi there"); D) System.out.println("\"hi there\""); 3) Consider the following statement: System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night"); This statement will output ________ lines of text A) 1 B) 2 C) 3 D) 4 E) 5 4) What is output with the statement System.out.println(x+y); if x and y are int values where x=10 and y=5? A) 15 B) 105 C) 10 5 D) x+y E) An error since neither x nor y is a String 5) What is output with the statement System.out.println(""+x+y); if x and y are int values where x=10 and y=5? A) 15 B) 105 C) 10 5 D) x+y E) An error since neither x nor y is a String
Write a program that generates the following output
2|Page
public class myProgram { System.out.println(“My First program in \”java\” \n ———————”); System.out.print(“3+2= ”+(3+2)); }
Prepared by Mrs.Enas
Departments: CS Course:Programming (CS110DT) Class: Level 1
KINGDOM OF SAUDI ARABIA PRINCESS NORA UNIVERSITY FACULTY OF COMPUTER AND
INFORMATION SCIENCES
Lecture 3 – Tutorial 1. What is the output of the following programs? public class PondRadius { public static void main(String[] args) { int fishCount = 20; int fishLength = 10; int inchesPerFoot = 12; int lengthPerSqFt = 2; double radius = 0.0; int feet = 0; int inches = 0; double pondArea = (double)(fishCount*fishLength)/lengthPerSqFt; radius = pondArea/3.14; feet = (int)(pondArea/ radius); System.out.println("To hold " + fishCount + " fish averaging " + fishLength + " inches long you need a pond with an area of \n" + pondArea + " square feet."); } }
To hold 20 fish averaging 10 inches long you need a pond with an area of 100.0 square feet.
public class Exercise { public static void main(String[] args) { short data = 5; data *= 2; System.out.println("data = " + data); data *= 2; System.out.println("data = " + data); }}
data= 10 data= 20
3|Page
Prepared by Mrs.Enas
KINGDOM OF SAUDI ARABIA PRINCESS NORA UNIVERSITY FACULTY OF COMPUTER AND
Departments: CS Course:Programming (CS110DT) Class: Level 1
INFORMATION SCIENCES
public class Welcom{ public static void main(String[] args){ int number1; double number2; float number3= 0.0f; int number4=0; number1= 5/2; number2= 5/2; System.out.println("\t Second Remedial Class\b"); System.out.println(number1 +"\t"+ number2 + "\t"+ number3 + "\t"+ number4); number1++; number4=number1; System.out.println(number1 +"\t"+ number2 + "\t"+ number3 + "\t"+ number4); System.out.println(number1++ +"\t"+ number2 + "\t"+ number3 + "\t"+ --number4); System.out.println(number1 +"\t"+ number2 + "\t"+ number3 + "\t"+ --number4); System.out.printf("%.2f \n", 2.4356433); System.out.printf("%20s language ", "programming"); System.out.printf("%.1s \n", "java"); }}
2 3 3 3 2.44
Second Remedial Class 2.5 0.0 0 2.5 0.0 3 2.5 0.0 2 2.5 0.0 1 programming language j
public class Exercise { public static void main(String[] args) { int ID=354323; double mark=19.5875; String subject="programming 1"; String name="Huda"; System.out.printf("%s\nMy name is : %s .\n My ID number is:%d\n I got %.2f ","********",name,ID,mark); }}
******** My name is: Huda. My ID number is:354323 I got 19.59 4|Page
Prepared by Mrs.Enas
KINGDOM OF SAUDI ARABIA PRINCESS NORA UNIVERSITY FACULTY OF COMPUTER AND
Departments: CS Course:Programming (CS110DT) Class: Level 1
INFORMATION SCIENCES
public class Exercise { public static void main(String[] args) { int ID=354323; double mark=19.5875; String subject="programming 1"; String code="110D"; System.out.printf("The subject is %s%15s\n",subject,code); System.out.printf("%-10s\n",subject); System.out.printf("%20d\n",ID); }}
public class Exercise { public static void main(String[] args) { int val1 = 3; int val2 = 5; System.out.printf("%d\n", val1); val1++; System.out.printf("%d\n", val1); System.out.printf("%d\n", val1++); System.out.printf("%d\n", val1); System.out.printf("%d\n", ++val1); System.out.printf("%d\n", --val2); System.out.printf("%d\n", val2); ++val2; System.out.printf("%d\n", val2); }}
5|Page
Prepared by Mrs.Enas
KINGDOM OF SAUDI ARABIA PRINCESS NORA UNIVERSITY FACULTY OF COMPUTER AND
Departments: CS Course:Programming (CS110DT) Class: Level 1
INFORMATION SCIENCES
public class Exercise { public static void main(String[] args) { int x,y,z; x=4; y=2; z=x+y; y+=10; System.out.println(z + " " + y + " " +x); x*=5; System.out.println(z + " " + y++ + " " +x); x=z; z=y; System.out.println(--z + " " + y + " " +x); }}
2. Find the error in the following code public class Paint{ public static void main(String[] args)\ { double height1=10; double height2=10; int width1=18.5; double width2=20.5; double squareFeet; int numGallons; in umGallons; numGallons=squareFeet/10; squareFeet=(width1*height1+width2*height2)*2; System.out.println(Number of Gallons: + numGallons); } }
6|Page
Prepared by Mrs.Enas
Departments: CS Course:Programming (CS110DT) Class: Level 1
KINGDOM OF SAUDI ARABIA PRINCESS NORA UNIVERSITY FACULTY OF COMPUTER AND
INFORMATION SCIENCES
3. Determine which of the following variables Names is valid or not and explain why? Name of Variable
Valid/not valid
Reason
3wrong $don’t public myAge Number Two 1number Number This_is_a_var May25 $number %intNum NUMBER Float 123_alpha 4. Declare a variable to store a product number (1- 1000) and choose meaningful variable names. 5. Declare a variable to hold the character ( C); 6. Declare a Boolean variable initialized to true. 7. Declare a variable to store the number of pets in your family. and choose meaningful variable names. 8. Declare a variable to store the price of a pair of boots and choose meaningful variable names. 9. Declare a variable to store the name of your favorite book. and choose meaningful variable names. 10. Declae and initialize an interger variable named currentYear.Initilize this variable with the value of the current year. Write the appropriate code statement to output the content of currentYear variable. 11. Declare and initialize constants to store the rice of a care wash is 14.95. 12. Declare and initialize a constant variable to store the name of your dog “Duchess”. 7|Page
Prepared by Mrs.Enas
Departments: CS Course:Programming (CS110DT) Class: Level 1
KINGDOM OF SAUDI ARABIA PRINCESS NORA UNIVERSITY FACULTY OF COMPUTER AND
INFORMATION SCIENCES
13. Give the values of variable f after each of the following statements int a=5, b=10, f; a. f =++a – b; -4 b. f=a-- -b; -5 c. f=++a +b--; 16 14. Find the values of the variable fee and rem after the following statements are executed. double fee=5.5; int rem =150; fee *=10.5; rem%=10
15. Identify the errors, if any, in the following statements; int x=10 , y=20; float z =30.5f; a. int index(float) z; b. float salary =(int) x*1000.5f); c. int value=(int)z; d. Boolean isRed=10; e. X=x-100++; f. Y=--(x*y); g. System.out.print(“x=” x); 16. If x is an int and y is a float, all of the following are legal assignment statement? a. y = x; b. x = y; c. y = (float) x; d. x = (int) y; e. all of the above are legal
Write the following programs. A) Write a program that asks the users to enter three integer numbers. Then print the average of these numbers using printf method? B) Modify the privious code so that the program print the average without truncating the decimal numbers. Nore : average=(number1+ number1+ number1)/3.
8|Page
Prepared by Mrs.Enas
KINGDOM OF SAUDI ARABIA PRINCESS NORA UNIVERSITY FACULTY OF COMPUTER AND
Departments: CS Course:Programming (CS110DT) Class: Level 1
INFORMATION SCIENCES
C) Write a program that calculates and output a user age in the year of 2040 Note: 2040 should be stored in a constant variable . The program should prompt the user to enter his/her age To calculate the age in 2040 use the formula : newAge= currentAge+(2040 – currentyear) public class calcualte_age { public static void main(string[] args) { Scanner input=new Scanner(system.in) final int year = 2040; int current_year=2016; int current_age, new_age; current_age=input.nextInt(); new_age = current_age+(year - current_year); System.out.print(“your age in 2040 is: ”+ new_age); } }
D) Write a program that prompts the user to enter a value of radius, and compute the surface area of a sphere. Note: area of a sphere = 4 pi radius2, where pi is 3.14 public class sphere { public static void main(string[] args) { Scanner input=new Scanner(system.in) final double pi = 3.14; double area; int radius; System.out.print(“please enter the radius:”); radius= input.nextInt(); area = 4*pi* radius* radius; System.out.print(“area of a sphere is : ”+area); } }
9|Page
Prepared by Mrs.Enas