🌙 DARK

Simplified Fraction - Sum of an array - Command Line Argument - Print String

 Simplified Fraction

 

St. Patrick Convent organizes a project exhibition "Innovative Minds" every year with an objective to provide the platform and unleash the potential of the students by showcasing their innovative projects. Pasha is a smart high school student and was eager to participate in the fair for the first time.
 
After a lot of ground works, she decided her project and set out to design the same. Her project requirement was to design an advanced calculator that has a fraction feature that will simplify fractions. The project will accept a non-negative integer as a numerator and a positive integer as a denominator and outputs the fraction in simplest form. That is, the fraction cannot be reduced any further, and the numerator will be less than the denominator.
 
Help Pasha to program her advanced calculator and succeed in her first ever project presentation. You can assume that all input numerators and denominators will produce valid fractions.


Hence create a class named Fraction with the following method.

 

Method NameDescription
void printValue(int,int)This method should display the fraction in simplest form.

 

Create a driver class called Main. In the Main method, obtain input from the user in the console and call the printValue method present in the Fraction class.
 
[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem statement. Create separate classes in separate files.]


Input Format:
First line of the input is a non-negative integer which is the numerator in the fraction.
Second line of the input is a positive integer which is thedenominator in the fraction.

Output Format:
Output the simplified form of the fraction in a single line.
Refer sample input and output for formatting specifications.
 
Sample Input 1:
28
7

Sample Output 1:
4

Sample Input 2:
13
5

Sample Output 2:
2 3/5


PROGRAM:



public class Fraction{
    public static void printValue (int num,int den)
    {
            if(num==0)
            {
                System.out.println(num);
            }
            else if((num%den)==0)
            {
                System.out.println(num/den);
            }
           
           else{
               int hcf = findHCF(num,den);
               
               if(hcf==1)
               {
                   if(num>den)
                   {
                       System.out.println(num/den+" "+num%den+"/"+den);
                   }
                   else
                   {
                       System.out.println(num+"/"+den);
                   }
               }
               
               else{
                   num = num/hcf;
                   den = den/hcf;
                   if(num>den)
                   {
                       System.out.println(num/den+" "+num%den+"/"+den);
                   }
                   else
                   {
                       System.out.println(num+"/"+den);
                   }
               }
           }
           
    }
           public static int findHCF(int num,int den)
    {
        int hcf=1;
        for(int i=1;i<=num||i<=den;i++)
        {
            if(num%i==0 && den%i==0)
                hcf = i;
        }
        return hcf;
      
    }  
    
}




import java.util.*;
public class Main{
    public static void main(String args[]){
       // Fraction obj = new Fraction();
        Scanner sc = new Scanner(System.in);
       // System.out.println("")
        int num = sc.nextInt();
        
       // System.out.println("")
        int den = sc.nextInt();
        Fraction.printValue (num,den);
    }
}









SUM OF AN ARRAY

  Sum of an array


Write a program to find the sum of the elements in an array using for each loop.

Input Format:

Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the array. Assume that the maximum value of n is 15.

Output Format:

Refer sample output for details.

All text in bold corresponds to the input and remaining corresponds to the output.


Sample Input 1:

Enter n :
5
2
3
6
8
1


Sample Output 1:

Sum of array elements is : 20

Sample Input 2:

Enter n :
4
5
9
45
14


Sample Output 2:

Sum of array elements is : 73



 




Problem Requirements:

Java

KeywordMin CountMax Count
for12


PROGRAM:

import java.io.*; 
import java.util.*; 
public class Main{ 
public static void main (String[] args) throws Exception{ 
        
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter n :");
       int n = sc.nextInt();
       int sum = 0;
       int arr[] = new int [15];
       
       for (int i = 0; i < n; i++) { 
          
            arr[i]=sc.nextInt();
            sum += arr[i];
        }  
        
        System.out.println("Sum of array elements is : " + sum);  



COMMAND LINE ARGUMENT - PRINT STRING

Command Line Argument - Print String

Write a program to accept a string as a command-line argument and print the same.

Sample Input (Command Line Argument) 1:
Programming

Sample Output 1:
Programming - Command Line Arguments

Sample Input (Command Line Argument) 2:
Arguments

Sample Output 2:
Arguments - Command Line Arguments
 

PROGRAM:

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        //Scanner sc = new Scanner(System.in);
        for(int i=0; i<args.length; i++){
            System.out.println(args[i]+"- Command Line Arguments");
        }
}




Method Overloading – Payment Details
[Note :
Strictly adhere to the object oriented specifications given as a part of the problem statement.
Follow the naming conventions as mentioned. Create separate classes in separate files.]


Write a program to display the details based on the payment mode used for ticket booking using method overloading.

Create class Ticket Booking and overload the makePayment( ) method as follows,

Method
makePayment(double amount)
makePayment(String walletNumber, double amount)
makePayment(String creditCard, String ccv, String name, double amount)


Create Main class with main method, get the mode of payment from the user. 
Based on the payment mode, get the payment mode details and call the corresponding method and display the payment details.

 
Input and Output Format:
Refer sample input and output for formatting specifications.
All text in bold corresponds to the input and the rest corresponds to output.

Sample Input and Output 1:
Enter the mode of Payment:
1.Cash Payment
2.Wallet Payment
3.Credit Card
1
Enter the Amount of Payment:
5000
You have selected the Cash payment mode
The Amount is Rs.5000

Sample Input and Output 2:
Enter the mode of Payment:
1.Cash Payment
2.Wallet Payment
3.Credit Card
2
Enter the Wallet Number:
GRB1083
Enter the Amount of Payment:
2000
You have selected the Wallet payment mode
Wallet Number: GRB1083
The Amount is Rs.2000

Sample Input and Output 3:
Enter the mode of Payment:
1.Cash Payment
2.Wallet Payment
3.Credit Card
3
Enter the Credit Card Number:
5105105105105100
Enter the Validity Date(dd/MM/yyyy):
25/11/2020
Enter the Card Holder Name:
John Kennedy
Enter the Amount of Payment:
50000
You have selected the Credit Card payment mode
CreditCard Number:5105105105105100
Validity Date:25/11/2020
Card Holder Name: John Kennedy
The Amount is Rs.50000

Sample Input and Output 4:
Enter the mode of Payment:
1.Cash Payment
2.Wallet Payment
3.Credit Card
4
Please select the correct mode of payment...


PROGRAM:

import java.text.*;


public class TicketBooking{

   public int amount;
    public String walletNumber,creditCard,name,ccv;
public double getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}

public String getWalletNumber() {
return walletNumber;
}

public void setWalletNumber(String walletNumber) {
this.walletNumber = walletNumber;
}

public String getCreditCard() {
return creditCard;
}

public void setCreditCard(String creditCard) {
this.creditCard = creditCard;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCcv() {
return ccv;
}

public void setCcv(String ccv) {
this.ccv = ccv;
}

public void MakePayment(int amount){

System.out.println("You have selected the Cash payment mode");
    System.out.println("The Amount is Rs."+amount);
}

    public void MakePayment1(String walletNumber, int amount1){

System.out.println("You have selected the Wallet payment mode");
    System.out.println("Wallet Number: "+walletNumber);
    System.out.println("The Amount is Rs."+amount1);
   
    }

   public void MakePayment2(String creditCard, String ccv, String name, int amount2){
System.out.println("You have selected the Credit Card payment mode");
    System.out.println("CreditCard Number: "+creditCard);
    System.out.println("Validity Date: "+ccv);
System.out.println("Card Holder Name: "+name);
    System.out.println("The Amount is Rs."+amount2);
    }

}


CLASS 2: 

import java.io.*;
import java.util.*;
public class Main {
  public static void main(String[] args) throws Exception, IOException {
    Scanner sc = new Scanner(System.in);
System.out.println("Enter the mode of Payment:\n1.Cash Payment\n2.Wallet Payment\n3.Credit Card");
int mode = sc.nextInt();
TicketBooking obj = new TicketBooking();
        switch (mode){
            case 1:
            System.out.println("Enter the Amount of Payment:");
            int amount = sc.nextInt();
            //obj.setAmount(amount);
            obj.MakePayment(amount);
            break;

            case 2:
            System.out.println("Enter the Wallet Number:");
            String walletNumber = sc.nextLine();
            System.out.println("Enter the Amount of Payment:");
            int amount1 = sc.nextInt();
            obj.MakePayment1(walletNumber, amount1);

               //obj.setWalletNumber(walletNumber);
               //obj.setAmount(amount1);
               break;

            case 3:
              System.out.println("Enter the Credit Card Number:");
              String creditCard = sc.nextLine();
              System.out.println("Enter the Validity Date(dd/MM/yyyy):");
              String ccv = sc.nextLine();
              System.out.println("Enter the Card Holder Name:");
              String name = sc.nextLine();
              System.out.println("Enter the Amount of Payment:");
              int amount2 = sc.nextInt();
              obj.MakePayment2(creditCard, ccv, name, amount2);
              /*obj.setCreditCard(creditCard);
              obj.setCcv(ccv);
              obj.setName(name);
              obj.setAmount(amount2);*/
              break;
              

            case 4:
            System.out.println("Please select the correct mode of payment...");
              
        }
}
}

THE END

Post a Comment

Thanks for reading the blog. We hope it was useful to you and that you learned something new. Will always be writing on new and interesting topics, so you can visit our website to know the latest updates of our blogs. Thank You!

Previous Post Next Post

Contact Form