CSC 2513 - Udit Kumar Mahato

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

There are FOUR (4) questions in this section.

Answer ALL Questions in the ANSWER BOOKLET.


1. a. Explain the differences between while loop and do-while loop with
example. (4 marks)
Ans:-The differences between while loop and do-while loop are as follows:-
While Loop Do-While Loop
It checks condition first then execute It executes Statement first then checks
Statement the condition
Statement might be executed Zero Statement is executed at least one
times. times.
No semicolon at the end of while. Semicolon at the end of while.
Variable in condition is initialized variable may be initialized before
before the execution of loop. or within the loop.
If there is single statement ,brackets are Brackets are always required.
not required.

Example of while loop:


// Program to display numbers from 1 to 5

class While {
public static void main(String[] args) {

// declare variables
int i = 1, n = 5;

// while loop from 1 to 5


while(i <= n) {
System.out.println(i);
i++;
}
}
}
Example of Do-While loop:

// Java Program to display numbers from 1 to 5

import java.util.Scanner;

// Program to find the sum of natural numbers from 1 to 100.

class DoWhile {
public static void main(String[] args) {

int i = 1, n = 5;

// do...while loop from 1 to 5


do {
System.out.println(i);
i++;
} while(i <= n);
}
}
b. Write JAVA codes based on the output given below:

Code:
//compare two numbers
import java.util.Scanner;
public class CheckGreaterSmaller {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//to take first num
System.out.print("Enter the first number : ");
int firstNum=input.nextInt();
//to take second num
System.out.print("Enter the second number : ");
int SecondNum=input.nextInt();
//If else statement to compare the two numbers
if(SecondNum>firstNum){
System.out.println(SecondNum+" is greater than "+firstNum);
}
else{
System.out.println(firstNum+" is greater than "+SecondNum);
}
}
}
Output:

2. Write JAVA program with following:


- Accept a string as user input
- Convert the string into lower case
- Extract the second character from the string
- Use switch statement to test whether the extracted character is vowel or
consonant.
The output should be in following format:

Code:
import java.util.Scanner;
//Main class created
public class TakeInputString {
public static void main(String[ ] arg)
{
//Created boolean statement
boolean isVowel=false;;
Scanner scanner=new Scanner(System.in);
//To take input from user
System.out.print("Enter a string with length more than 2 : ");
String Word=scanner.next();
//Convert String to Lowercase
String Converted=Word.toLowerCase();
//To know the letter that is second character of the given string
char index = Converted.charAt(1);
//Closed Scanner
scanner.close();
//loop to decide either the second character is vowel or Constant
switch(index)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' : isVowel = true;
}
if(isVowel == true) {
System.out.println(index +" is a Vowel");
}
else {
if((index >='a'&&index <='z')||(index>='A'&&index <='Z'))
System.out.println(index +" is a Consonant");
else
System.out.println("Input is not an alphabet");
}
}
}

Output:

3. A shopping mart named “STAR GROCERY” is providing a discount to its


customer based on their accumulated purchase of grocery items. The discount
is given based on the total purchase price as given below:
Your program should include the following:
- Use looping to accept the purchase amount.
- Calculate total purchase amount, and calculate total discount and amount
payable after discount.
- Ask the user to enter the customer name
- Use java file handling, to prepare a file called ‘invoice.txt’.
The file invoice.txt should contain following information :

The sample output should as below:


Code:
import java.util.Scanner;
import java.io.*;
public class InvoiceStarGrocery {
public static void main(String[] args)throws FileNotFoundException {
PrintWriter out = new PrintWriter("invoice.txt");
Scanner sc = new Scanner(System.in);
char ex;
double PurchaseAmt=0;
double TotalPurchase = 0;
double discount = 0;
double payableAmt = 0;
do {
System.out.print("Enter the purchase amount in Rs : ");
PurchaseAmt = sc.nextFloat();
if(PurchaseAmt!=0){
TotalPurchase=TotalPurchase+PurchaseAmt;
}
else {

}
System.out.println("\n\n More Purchases(y/n)?");
ex = sc.next().charAt(0);
} while (ex == 'y');
if(TotalPurchase<=100.99 && TotalPurchase>0){
discount=0.05*TotalPurchase;
}
else if(TotalPurchase<=200.99){
discount=0.08*TotalPurchase;
}
else if(TotalPurchase>201){
discount=0.10*TotalPurchase;
}
else{
System.out.println("Wrong Amount");
}
payableAmt=TotalPurchase-discount;
Scanner n=new Scanner(System.in);
System.out.print("Enter customer name for billing: ");
String name=n.nextLine();
out.printf("\n"+"Customer Name : "+name);
out.printf("\n"+"Total purchase amount(Rs.) "+TotalPurchase);
out.printf("\n"+"Discount Amount(Rs.) "+discount);
out.printf("\n"+"Amount payable after discount(Rs. ) "+payableAmt);
sc.close();
n.close();
out.close();
}
}

Output:

Textfile:

4. Write java program based on the following class diagram:


Following are the details of method used:
- The constructor bank should initialize the name of customer and initial
balance of Rs.1000
- deposit(double) - should add the deposited amount to customer’s given
balance amount
- withdraw(double) - the function should check if customer has sufficient
balance, if True subtract the amount from customer’s balance amount
otherwise display information “insufficient balance available”
- display() - function should show complete user information. The output
should be in following format:

Code:
//bank services java program
import java.util.Scanner;
public class Bank {
String Customer_name;
double balance;
//Created constructor named bank
Bank(String name){
Customer_name = name;
balance = 1000;
}
//Method for deposit
public void deposit(double depositamt){
balance+=depositamt;
}
//method to withdraw
public void withdraw(double withdrawamt){
if (withdrawamt>balance){
System.out.println("Sufficient Balance");
}
else {
balance -= withdrawamt;
System.out.println(withdrawamt + " successfully withdrawn");
}
}
//Method to checkBalance
public void checkBalance(){
System.out.println("Balance: " + balance);
}
//method to print customer name and available balance
public void display(){
System.out.println("\nName: " + Customer_name);
System.out.println("Amount Available: Rs. " + balance);
}
//Main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Bank b1 = new Bank("Sunway College");
b1.display();
System.out.print("\nEnter deposit amount: Rs. " );
double deposit = sc.nextDouble();
b1.deposit(deposit);
System.out.println(deposit+" successfully deposited");
System.out.print("\nEnter withdraw amount: Rs. ");
double withdraw = sc.nextDouble();
b1.withdraw(withdraw);
b1.display();
}
}

Output:

You might also like