Tuesday, 7 May 2013

JAVA on Flow Controls (if-else/switch case)

( I ) CheckPassFail (if-else):

Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal to 50; or prints "FAIL" otherwise.

Hints : [Recommended IDE - Eclipse Ganymede 32 bit]
public class CheckPassFail { // saved as "CheckPassFail.java"
public static void main(String[] args) {
int mark = 49; // set the value of mark here!
System.out.println("The mark is " + mark);
if ( ...... ) {
System.out.println( ...... );
} else {
System.out.println( ...... );
}
}
}


Download the Code at -
https://docs.google.com/file/d/0B8MoFjuZZZwjUTNLVjd5Tm5OSVU/edit?usp=sharing
=========================================================================
 
( II )Exercise CheckOddEven (if-else): 
 
Write a program called CheckOddEven which prints "Odd Number" if the int variable “number” is odd, or “Even Number” otherwise.
Hints: n is an even number if (n % 2) is 0.

public class CheckOddEven { // saved as "CheckOddEven.java"
public static void main(String[] args) {
int number = 49; // set the value of number here!
System.out.println("The number is " + number);
if ( ...... ) {
System.out.println( ...... );
} else {
System.out.println( ...... );
}
}
}

Download the sample code at –
https://docs.google.com/file/d/0B8MoFjuZZZwjc2owbHNQWUMyLWs/edit?usp=sharing
======================================================================
 
( III )PrintNumberInWord (nested-if, switch-case):
 
Write a program called PrintNumberInWord which prints "ONE", "TWO",... , "NINE", "OTHER" if the int variable "number" is 1, 2,... , 9, or other, respectively. 
Use (a) a "nested-if" statement; (b) a "switch-case" statement.

Hints:
public class PrintNumberInWord
{ //saved as"PrintNumberInWord.java"
public static void main(String[] args)
{
int number = 5;
// Using nested-if
if (number == 1) {
System.out.println("ONE");
} else if (......) {
......
} else if (......) {
......
......
} else {
......
}
// Using switch-case
switch(number) {
case 1: System.out.println("ONE"); break;
case 2: ......
......
......
default: System.out.println("OTHER");
}
}


Download the Code here –
https://docs.google.com/file/d/0B8MoFjuZZZwjWEVqdldNVWpzcTA/edit?usp=sharing
=====================================================================
 
( IV )PrintDayInWord

Write a program called PrintDayInWord, which prints “Sunday”, “Monday”, ... “Saturday” if the int variable "day" is 0, 1, ..., 6, respectively. Otherwise, it shall print “Not a valid day”. This one is your practice problem. 
Sample Code to Download -  
https://docs.google.com/file/d/0B8MoFjuZZZwjS2p5aWlSbG5jaWs/edit?usp=sharing =======================================================================

4 comments:

  1. hi...dear...can i join your site

    ReplyDelete
    Replies
    1. Yeah sure.
      But, what do you want to ask exactly?
      I mean, what do you mean by "join"?

      Delete
  2. Replies
    1. Dear Neetu, the run program is already there..just click on the link.
      try it, it should work properly. Thanks for the query.

      Delete