Computer Applications
Hero Honda has increased the cost of its vehicles as per the type of the engine using the following criteria:
Type of Engine | Rate of increment |
---|---|
2 stroke | 10% of the cost |
4 stroke | 12% of the cost |
Write a program by using a class to find the new cost as per the given specifications:
Class name: Honda
Data Members | Purpose |
---|---|
int type | To accept type of engine 2 stroke or 4 stroke |
int cost | To accept previous cost |
Member Methods | Purpose |
---|---|
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
Define a class called 'Mobike' with the following specifications:
Data Members Purpose int bno To store the bike number int phno To store the phone number of the customer String name To store the name of the customer int days To store the number of days the bike is taken on rent int charge To calculate and store the rental charge Member Methods Purpose void input() To input and store the details of the customer void compute() To compute the rental charge void display() To display the details in the given format The rent for a mobike is charged on the following basis:
Days Charge For first five days ₹500 per day For next five days ₹400 per day Rest of the days ₹200 per day Output:
Bike No. Phone No. Name No. of days Charge xxxxxxx xxxxxxxx xxxx xxx xxxxxx
Write a program using a class with the following specifications:
Class name: Caseconvert
Data Members Purpose String str To store the string Member Methods Purpose void getstr() to accept a string void convert() to obtain a string after converting each upper case letter into lower case and vice versa void display() to print the converted string Define a class Library having the following description:
Class name : Library
Data Members Purpose String name to store name of the book int price to store the printed price of the book int day to store the number of days for which fine is to be paid double fine to store the fine to be paid Member functions Purpose void input() To accept the name of the book and printed price of the book void cal() Calculates the fine to be paid void display() Displays the name of the book and fine to be paid Write a program to compute the fine according to the given conditions and display the fine to be paid.
Days Fine First seven days 25 paise per day Eight to fifteen days 40 paise per day Sixteen to thirty days 60 paise per day More than thirty days 80 paise per day Bank charges interest for the vehicle loan as given below:
Number of years Rate of interest Up to 5 years 15% More than 5 and up to 10 years 12% Above 10 years 10% Write a program to model a class with the specifications given below:
Class name: Loan
Data Members Purpose int time Time for which loan is sanctioned double principal Amount sanctioned double rate Rate of interest double interest To store the interest double amt Amount to pay after given time Member Methods Purpose void getdata() To accept principal and time void calculate() To find interest and amount.
Interest = (Principal*Rate*Time)/100
Amount = Principal + Interestvoid display() To display interest and amount