Choong Kar Yee
182630
1.
public class test1 {
public static void main (String[] args) {
int x = 8000000;
while ( x > 0 )
x++;
System.out.println( “x is “ + x );
}
}
asnwer is -2147483648. A variable being assigned to the largest value, answer will eventually reached the maximum which is 2147483647. &
2.
public class test2 {
public static void main (String[] args) {
int s = 1, n = 0;
boolean b;
while (n < 10)
{
n++;
s += n;
b = (n % 2==0);
System.out.println( " n = " + n + " and b = " + b);
if (s > 5)
{
System.out.println( “s = “ +s);
break;
}
}
}
}
3.
import java.util.Scanner;
public class Test3{
public static void main (String []args){
Scanner input = new Scanner(System.in);
int number;
int sum = 0;
do
{
System.out.println("Enter an integer. The input ends if it is 0. ");
number = input.nextInt();
sum += number;
}
while(number != 0 );
System.out.println("Enter an integer. The input ends if it is 0.");
}
}
4.
public class test4 {
public static void main (String[] args) {
for (int count=1;count<=100;count++)
System.out.println( count );
}
}
5.
while loop:
long sum = 0;
int i = 0;
while (i <= 1000);
int i=input.nextInt();
sum + = i++;
do-while loop:
long sum=0;
int i=0;
do
{
sum+=i++;
}
while (i<=1000);
6.
i is 0, printIn statement executed.
i is 1, printIn statement executed once.
i is 2, printIn statement executed twice.
...
i is 9, printIn statement executed nine times.
Total : 0+1+2+3+4+5+6+7+8+9=45
7.
import java.util.Scanner;
public class Test7{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter a String");
String string=input.nextLine();
int vowels=0;
int consonants=0;
for(int i=0; i<string.length();i++){
if(Character.isLetter(string.charAt(i))){
if(Character.toUpperCase(string.charAt(i))=='A'||
Character.toUpperCase(string.charAt(i))=='E'||
Character.toUpperCase(string.charAt(i))=='I'||
Character.toUpperCase(string.charAt(i))=='O'||
Character.toUpperCase(string.charAt(i))=='U')
{
vowels++;
}
else
consonants++;
}
System.out.println("The number of vowels is" + vowels );
System.out.println("the number of consonants is" + consonants);
}
}
}
8.
import java.util.Scanner;
public class Test8{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int highestScore=0;
String highestScoreName="";
System.out.println("Enter number of students:");
int numberOfStudents=input.nextInt();
System.out.println("Enter student's name and test 1 score");
for(int i=0;i<numberOfStudents;i++){
System.out.print("Student"+(i+1)+"\n Name: ");
String name=input.next();
System.out.print("Score is");
int score=input.nextInt();
if(score>highestScore)
{
highestScore=score;
highestScoreName=name;
}
}
System.out.println("Student with highest score is " + highestScoreName);
}
}
9
import java.util.Scanner;
public class Test9{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int computer,player;
int point=1,computerPoint=0,playerPoint=0;
System.out.println(" Rock paper scissor game");
while((playerPoint!=3)&&(computerPoint!=3))
{
System.out.println("Enter 1-Scissor 2-Rock 3-Paper");
player=input.nextInt();
computer=(1+(int)(Math.random()*3));
if((computer==1)&&(player==3))
{
System.out.println("Scissor vs Paper. You lose");
computerPoint+=point;
}
else if((computer==2)&&(player==1))
{
System.out.println("Rock vs Scissor.You lose");
computerPoint+=point;
}
else if((computer==3)&&(player==2))
{
System.out.println("Paper vs Rock.You lose");
computerPoint+=point;
}
else if((computer==1)&&(player==2))
{
System.out.println("Scissor vs Rock.You win");
playerPoint+=point;
}
else if((computer==2)&&(player==3))
{
System.out.println("Rock vs paper.You win");
playerPoint+=point;
}
else if((computer==3)&&(player==1))
{
System.out.println("Paper Scissor.You win");
playerPoint+=point;
}
else if((computer==1)&&(player==1))
{
System.out.println("Scissor vs Scissor.Draw");
}
else if((computer==2)&&(player==2))
{
System.out.println("Rock vs Rock.Draw");
}
else if((computer==3)&&(player==3))
{
System.out.println("Paper vs Paper.Draw");
}
else
System.out.println(" Please choose between 1,2,3");
}
if(computerPoint>=3)
System.out.println("You lose");
else if(playerPoint>=3)
System.out.println("You Win");
}
}
10
Phewwwww..... That is all for today lab!