Computer Applications

Hero Honda has increased the cost of its vehicles as per the type of the engine using the following criteria:

Type of EngineRate of increment
2 stroke10% of the cost
4 stroke12% of the cost

Write a program by using a class to find the new cost as per the given specifications:

Class name: Honda

Data MembersPurpose
int typeTo accept type of engine 2 stroke or 4 stroke
int costTo accept previous cost
Member MethodsPurpose
void gettype()To accept the type of engine and previous cost
void find()To find the new cost as per the criteria given above
void printcost()To print the type and new cost of the vehicle

Java

Java Classes

31 Likes

Answer

import java.util.Scanner;

public class Honda
{
    private int type;
    private int cost;
    private double newCost;
    
    public void gettype() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter type: ");
        type = in.nextInt();
        System.out.print("Enter cost: ");
        cost = in.nextInt();
    }
    
    public void find() {
        switch (type) {
            case 2:
            newCost = cost + (cost * 0.1);
            break;
            
            case 4:
            newCost = cost + (cost * 0.12);
            break;
            
            default:
            System.out.println("Incorrect type");
            break;
        }
    }
    
    public void printcost() {
        System.out.println("Type: " + type);
        System.out.println("New cost: " + newCost);
    }
    
    public static void main(String args[]) {
        Honda obj = new Honda();
        obj.gettype();
        obj.find();
        obj.printcost();
    }
}

Variable Description Table

Program Explanation

Output

Answered By

18 Likes


Related Questions