Computer Science, asked by ShanayaTrivedi, 1 year ago

Please answer the 12th question. its java.

Attachments:

ShanayaTrivedi: please help!!!

Answers

Answered by QGP
3
import java.util.Scanner;
class mobike
{
    
    int bno;    //These are the required variables asked in the question
    int phno;
    String name;
    int days;
    int charge;
       
    
    void input()
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter Bike Number: ");
        bno = sc.nextInt();
        
        System.out.print("Enter Phone Number of Customer: ");
        phno = sc.nextInt();
        
        
        /* sc.nextInt() scans only one token. It does not scan a complete line.
         * So, if we used sc.nextLine() directly for input, it will scan the previous line and actually skip input.
         * As a workaround, we add a simple sc.nextLine() , and then use it again for input
         *
         * Feel free to experiment by removing this single sc.nextLine() statement
         */
        sc.nextLine();
        
        System.out.print("Enter Name of Customer: ");
        name = sc.nextLine();
        
        System.out.print("Enter Number of Days the Bike has been rented: ");
        days = sc.nextInt();
    }
    
    void computer()
    {
        charge=0;
        
        if(days<=5)     //If days<5, simply calculate the charge
        {
            charge = days*500;
        }
        else        //If days>5, then we now add the charge of first five days
        {
            charge = 5*500;
            
            if(days<=10)        //Here, if days<10, we add the charge of the whatever days are there above first five
            {
                charge += (days-5)*400;
            }
            else        //If days>10, we now simply add the charge of second phase of 5 days as well
            {
                charge += 5*400;    
                               
                charge += (days-10)*200;  //Adding the charge of days beyond the first 10 days                
            }
        }
    }
    
    void display()
    {
        System.out.println();
        
        
        /* System.out.format() is used because "\t" cannot provide appropriate formatting. Here is the mechanism of it:
         *
         * System.out.format("%16d %32s", int variable, String variable);
         *
         * %16d refers to 16 character spacing for an integer, and %32s refers to 32 character spacing for a String.
         *
         * If we use %-16d, then the integer will be left aligned, as I used. Feel free to experiment yourself.
         *
         */
        
        
        System.out.format("%-16s %-16s %-20s %-16s %-16s","Bike No.","Phone No.","Name" ,"No. of days","Charge");
        
       
        System.out.println(); //Printing a new line
        
        
        System.out.format("%-16d %-16d %-20s %-16d %-16d",bno,phno,name,days,charge);
    }
    
    /*The question does not ask for a main() function.
     * If you wish to run the program,I am also adding a main() function as comments.
     */
    
    /*
    public static void main(String[] args)
    {
        mobike ob = new mobike();
        
        ob.input();
        ob.computer();
        ob.input();
    }
    */
    
}

QGP: Oh Wait! I spotted an error.
QGP: In the main() function given as comments, the last code line is ob.input()
QGP: It should be instead ob.display()
QGP: So, the three lines there would be:
QGP: ob.input();
QGP: ob.computer();
QGP: ob.display();
QGP: These are the correct lines of the main function.
ShanayaTrivedi: yah....ok thankyou so much
ShanayaTrivedi: :))
Similar questions