4
MIT Africa Information Technology Initiative 1 MIT AITI Mobile Application Development in Java Lab 03: Control Structures Complete these problems and show your solutions to the instructors. Be prepared to explain your code. Create a new project in Eclipse named “lab03”. Part I: Written Exercises (20 Minutes) 1. Consider the following code (draw a flowchart diagram if it helps): if (x > 2) { if (y > 2) { int z = x + y; System.out.println("z is " + z); } } else { System.out.println("x is " + x); } What is the output if: a. x = 2 and y = 5? b. x = 3 and y = 1? c. x = 1 and y = 1? d. x = 4 and y = 3? 2. Suppose we have the following code: switch (x + 3) { case 1: y = 0; break; case 2: y = 10; break; case 4: y += 1; break; default: y = 1; break; } a. What is y equal to at after the switch statement if x = 3 and y = 5 entering the switch? b. What if x = 2 and y = 5 at the beginning? 3. What does the following code output, and how many times do we run through the loop body? int i = 1; while (i < 10) { if ((i++) % 2 == 0) { System.out.println(i); } }

Am Introduction to java as a programming language(java programming tutorials)

Embed Size (px)

DESCRIPTION

Java programming presentations By Daroko blog Do not just read java as a programmer, find projects and start making some Money, at DAROKO BLOG,WE Guide you through what you have learned in the classroom to a real business Environment, find java applications to a real business Environment, find also all IT Solutions and How you can apply them, find the best companies where you can get the IT jobs worldwide, Find java contract, Complete and start making some cash, find clients within your Country, refer and get paid when you complete the work. Not Just a contact, at daroko Blog(www.professionalbloggertricks.com/),you are also being taught How you can apply all IT related field in real world. Simply Google, Daroko Blog or visit (www.professionalbloggertricks.com/) to Know More about all these service now. Do not just learn and god, Apply them in real world

Citation preview

Page 1: Am Introduction to java as a programming language(java programming tutorials)

MIT Africa Information Technology Initiative    1 

MIT AITI Mobile Application Development in Java Lab 03: Control Structures  Complete these problems and show your solutions to the instructors. Be prepared to explain your code. Create a new project in Eclipse named “lab03”. Part I: Written Exercises (20 Minutes)  1. Consider the following code (draw a flowchart diagram if it helps):  if (x > 2) { if (y > 2) { int z = x + y; System.out.println("z is " + z); } } else { System.out.println("x is " + x); }

What is the output if: a. x = 2 and y = 5? b. x = 3 and y = 1? c. x = 1 and y = 1? d. x = 4 and y = 3? 

2. Suppose we have the following code: 

switch (x + 3) { case 1: y = 0; break; case 2: y = 10; break; case 4: y += 1; break; default: y = 1; break; }

a. What is y equal to at after the switch statement if x = 3 and y = 5 entering the switch? b. What if x = 2 and y = 5 at the beginning? 

3. What does the following code output, and how many times do we run through the loop body?  

int i = 1; while (i < 10) { if ((i++) % 2 == 0) { System.out.println(i); } }

Page 2: Am Introduction to java as a programming language(java programming tutorials)

MIT Africa Information Technology Initiative    2 

4. How about this version of the code?  int i = 1; while (i > 10) { if ((i++) % 2 == 0) { System.out.println(i); } } Part II Computer Exercises:  Create a new class called UsingControlStructures in your lab03 project.  You will be submitting this java file, so be sure that everything works before you submit your lab.  There will be instructions below to mark the different questions on this part of the lab.  At the very top of the file, insert  

import java.util.*;

After the above line, copy the following code into your class:  public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter an integer: ”); int theInput = scanner.nextInt(); //Your code here } You may not understand the details of the above code. Don’t worry.  It suffices to know that scanner.nextInt() waits for the user to type an integer and press the “Enter” key, then returns what they entered as an int. In the code we store the value in an integer variable called theInput. 5. Now insert code into the code above so that the program prints “even” if the input integer is even 

and “odd” if it is odd.   

6. Suppose that we want to check if theInput is equal to 0, 1, or something else.  If it is equal to 0, have the program print “Zero”; if it is equal to 1, have the program print “One”; otherwise, have the program print “Something else.”  Do not use an if statement.

For problem 7‐10, use the same Java file and main as the previous problems.  Separate the output for each of the following problem problems by printing  “‐‐‐‐‐‐‐‐‐” to the screen. 

7. Declare and initialize variables representing:  

a. The age people start primary school, example: int primarySchoolAge = 4;

Page 3: Am Introduction to java as a programming language(java programming tutorials)

MIT Africa Information Technology Initiative    3 

b. The legal voting age. c. The age you can become president. d. The official retirement age. e. A person’s age.  Do this by using:  

System.out.print(“Enter an age:”); int personsAge = scanner.nextInt();

 Again, this just waits for the user to enter a number and then stores it in some variable.   

 In the main method, use if-else statements to print: f. “Too young.” if the person is too young for school. g. “Remember to vote” if the person is old enough to vote. h. “Vote for me” if the person is old enough to be president and “You can’t be president” if 

they are not. i. “Too old.” if the person is old enough to retire.

8. In the main method, write a for or while loop that prints out all the multiples of 3 down from 40 

to 0 in decreasing order. That is, 39, 36, 33, …, 3, 0. 

9. In the main method, write a loop that prints out all numbers between 6 and 30 that are not divisible by 2, 3, or 5.  

10. Using a while loop, find the smallest positive integer n such that 79*n has a remainder of 1 when 

divided by 97.  If you do not finish these in the allotted lab time, that is okay; you can go back at any point later in the course and submit these problems.  Please make a new Java file for problems 1, 2, and 3 named Lab03_ec1.java, Lab03_ec2.java, Lab03_ec3.java.  Don't hesitate to ask for clarifications or help!  1. Suppose we want to send some numbers over a phone line, but the phone line is tapped.  We want 

to use some (very simple) encryption to stop them from stealing our information.    

One quick and easy way to do this is to reverse the digits of our number.  For example, 12345 becomes 54321.  Think about how we can use %10 in a loop to get the last digit, and how we can make the new number using a loop.  Use only basic control structures and operators (don't use String methods).  Code a small program that will take in a number (use the Scanner from this lab to get input) and print out the encrypted number.  

2. To make our encryption even better, we can take each digit d of our number, and replace it with ((d + 7) mod 10) before we make our new number.  (Note that mod is the same as %).   

   For example, if we encrypt 12345:  

Page 4: Am Introduction to java as a programming language(java programming tutorials)

MIT Africa Information Technology Initiative    4 

    12345 ==> 89012 (from the digit change) ==> 21098 (from reversing it)     1532972 ==> 8209649 ==> 9469028  

Add this code to problem 1 so that both versions of the encryption is displayed.  If you have any questions regarding what the problem is asking, don't hesitate to ask. 

 3. A number (consider only positive integers) is perfect if it is equal to the sum of its proper divisors 

(numbers that divide evenly into the number but are less than the number). For example, 6 is a perfect number, because its proper divisors are 1, 2, and 3 (note that we do not include the number itself ), and 1 + 2 + 3 = 6.  

 A number is deficient if the sum of its proper divisors is less than the number. For example, 8 is deficient, because its proper divisors are 1, 2, and 4, and 1 + 2 + 4 = 7, which is less than 8.   A number is abundant if the sum of its proper divisors is greater than the number. For example, 12 is abundant, because 1 + 2 + 3 + 4 + 6 = 16, which is greater than 12.  Write a program that prompts the user for a number, then determines whether the number is perfect, deficient, or abundant. Your program should continue to prompt the user for numbers until a 0 is provided as input. An example session:  

Enter an integer (0 to quit): 7 7 is deficient. Enter an integer (0 to quit): 12 12 is abundant. Enter an integer (0 to quit): 6 6 is perfect.

Enter an integer (0 to quit): 0 Goodbye!