Download Activity 1.9.1 While loop with Answer File

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COURSE NAME: ICS3U INTRODUCTION TO COMPUTER SCIENCE
ICS3u Assessment as Learning 1.9.1
Student:
Teacher: Paul Pu
Curriculum expectations:
A2.2 use sequence, selection, and repetition control structures to create programming solutions;
Questions:
1. Count1.java Write a while loop that counts from 50 to 100 by 1.
package ics3u;
public class Count1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count=50;
while(count<100){
count=count+1;
System.out.println("count="+count);
}
}
}
2. Count2.java Write a while loop that counts from 0 to 50 by 5.
package ics3u;
public class Count2 {
public static void main(String[] args) {
int count=0;
while(count<50){
count=count+5;
System.out.println("count="+count);
}
}
}
3. Count3.java Write a while loop that counts down from 1000 to 200 by 100’s.
package ics3u;
public class Count3 {
public static void main(String[] args) {
int count=1000;
while(count>200){
count=count-100;
System.out.println("count="+count);
}
}
}
4. Count4.java Write a while loop that counts from a to b in steps of c, where a, b and
c are entered by the user.
package ics3u;
import java.util.Scanner;
public class Count4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("input count start:");
int a = in.nextInt();
System.out.println("input count end:");
int b = in.nextInt();
System.out.println("input step:");
int step=in.nextInt();
if(a<b){
while(a<b){
a=a+step;
System.out.println("a="+a);
}
}else{
while(a>b){
a=a-step;
System.out.println("a="+a);
}
}
}
}
5. Guess.java Write a while loop that allows the user to guess a number between 1
and 20 until they get it right.
6. Age.java Write a program that asks the user to guess your age. They must
continue guessing until they get it right. When the user guesses incorrectly, tell them
if they are too high or too low.
package ics3u;
import java.util.Random;
import java.util.Scanner;
public class Age{
public static void main(String[] args){
Random rand=new Random();
int numberToGuess=rand.nextInt(150);
int numberOfTries=0;
Scanner input=new Scanner(System.in);
int guess;
boolean win=false;
while(win==false){
System.out.println("Guess a number between 1 to 1000");
guess=input.nextInt();
numberOfTries++;
if(guess==numberToGuess){
win=true;
}
else if(guess<numberToGuess){
System.out.println("Your guess is too low");
}else if(guess>numberToGuess){
System.out.println("Your guess is too high");
}
}
System.out.println("You win");
System.out.println("The muber was "+numberToGuess);
System.out.println("It takes you "+numberOfTries+" trie");
}
}
7. LeastTwoPower.java Write a program that reads a positive integer and then finds
the smallest power of two that is greater than or equal to the number that is read.
For example, if the program reads the value 25, it should note that 32 = 25 is the
smallest power of two greater than or equal to 25.
package ics3u;
public class LeastTwoPower {
public static void main(String args[]){
double x = 1.0;
double number=Double.parseDouble(args[0]);
int n=0;
while(x<number){
x=x*2;
n++;
}
System.out.printf("2^%d = %.0f\n", n, x);
}
}