Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Instance Methods

Uploaded by

tejasmuradi1
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Instance Methods

Uploaded by

tejasmuradi1
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

------------------------------------INSTANCE METHOD----------------------

Q1.Write a program to input three numbers (positive or negative) using Scanner.


Numbers must be initialised to instance variables.
Create user defined method to pass the numbers as input and if they are unequal
then display the
greatest number otherwise, display they are equal.
The method also displays whether the numbers entered
by the user are "All positive", "All negative" or "Mixed numbers".
Sample Input: 56, -15, 12
Sample Output: The greatest number is 56

Entered numbers are mixed numbers.

ANS:
import java.util.Scanner;

public class MyProgram {


private int num1, num2, num3;

public void inputNumbers() {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");


num1 = scanner.nextInt();

System.out.print("Enter the second number: ");


num2 = scanner.nextInt();

System.out.print("Enter the third number: ");


num3 = scanner.nextInt();

scanner.close();
}

public void analyzeNumbers() {


int greatest = num1;

if (num2 > greatest) {


greatest = num2;
}

if (num3 > greatest) {


greatest = num3;
}

if (num1 == num2 && num2 == num3) {


System.out.println("The numbers are equal.");
} else {
System.out.println("The greatest number is " + greatest + ".");
}

if (num1 > 0 && num2 > 0 && num3 > 0) {


System.out.println("Entered numbers are all positive.");
} else if (num1 < 0 && num2 < 0 && num3 < 0) {
System.out.println("Entered numbers are all negative.");
} else {
System.out.println("Entered numbers are mixed numbers.");
}
}
public static void main(String[] args) {
MyProgram analyzer = new MyProgram();
analyzer.inputNumbers();
analyzer.analyzeNumbers();
}
}
-----------------------------------------------------------------------------------
---------------------
Q2.A triangle is said to be an "Equable Triangle', if the area of the triangle is
equal to its perimeter.
Write a program to enter three sides of a triangle. Create user defined method.
Check and print whether the triangle is equable or not.
For example, a right angled triangle with sides 5, 12 and 13 has its area and
perimeter
both equal to 30.

ANS:
import java.util.Scanner;

public class MyProgram {

public static double calculateArea(double s1, double s2, double s3) {


double s = (s1 + s2 + s3) / 2;
double area = Math.sqrt(s * (s - s1) * (s - s2) * (s - s3));
return area;
}

public static boolean checkEquableTriangle(double s1, double s2, double s3) {


double perimeter = s1 + s2 + s3;
double area = calculateArea(s1, s2, s3);
return area == perimeter;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the length of the first side: ");


double s1 = scanner.nextDouble();

System.out.print("Enter the length of the second side: ");


double s2 = scanner.nextDouble();

System.out.print("Enter the length of the third side: ");


double s3 = scanner.nextDouble();

scanner.close();

if (checkEquableTriangle(s1, s2, s3)) {


System.out.println("The triangle is equable.");
} else {
System.out.println("The triangle is not equable.");
}}
}
-----------------------------------------------------------------------------------
---------------------
Q3.A special two-digit number is such that when the sum of its digits is added to
the
product of its digits, the result is equal to the original two-digit number.
For Example, Consider the number 59.
Sum of digits: 5+9=14
Product of digits: 5*9=45
Total of the sum of digits and product of digits 14+45 = 59.
Write a program to create UDM that accept a two-digit number.
Add the sum of its digits to the product of its digits.
If the value is equal to the number input, display the message "Special 2-digit
number"
otherwise, display the message "Not a special two-digit number".

Ans :
import java.util.Scanner;

public class MyProgram {

public static boolean isSpecialNumber(int number) {


int sumOfDigits = number / 10 + number % 10;
int productOfDigits = (number / 10) * (number % 10);
return (sumOfDigits + productOfDigits) == number;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter a two-digit number: ");


int number = scanner.nextInt();

scanner.close();

if (number >= 10 && number <= 99) {


if (isSpecialNumber(number)) {
System.out.println("Special 2-digit number");
} else {
System.out.println("Not a special two-digit number");
}
} else {
System.out.println("Invalid input. Please enter a two-digit number.");
}
}
}
-----------------------------------------------------------------------------------
---------------------
Q4.The TSRTC Pushpak (Airport Electric Buses) is a service that operates air-
conditioned,
pollution-free, electric buses from and to Rajiv Gandhi International Airport
(R.G.I.A.)
in Hyderabad, Telangana. The buses have luxury seats, digital destination boards,
cell phone charging, and a wheelchair ramp. The service runs 24 hours a day,
with an average gap of 30 minutes between rides.
It has base fare based on the distance travelled as per the tariff given below:
Distance travelled Fare
upto 15km Fixed Charge Rs.70
16 km to 21km Rs 6/km
22km to 35km Rs 5/km
31km and above Rs 4/km
Develop an application with user defined method to input the distance travelled by
the passenger.
The method calculates and display the fare to be paid.
ANS:
import java.util.Scanner;

public class MyProgram {

public static double calculateFare(int distance) {


double fare;
if (distance <= 15) {
fare = 70;
} else if (distance <= 21) {
fare = 70 + (distance - 15) * 6;
} else if (distance <= 35) {
fare = 70 + 6 * (21 - 15) + (distance - 21) * 5;
} else {
fare = 70 + 6 * (21 - 15) + 5 * (35 - 21) + (distance - 35) * 4;
}
return fare;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the distance travelled (in km): ");


int distance = scanner.nextInt();

scanner.close();

if (distance > 0) {
double fare = calculateFare(distance);
System.out.println("The fare to be paid is: Rs. " + fare);
} else {
System.out.println("Invalid distance. Distance should be a positive
value.");
}
}
}
-----------------------------------------------------------------------------------
---------------------
Q5You have a saving account in a bank with some balance amount in your account.
Now, you want to perform the following tasks, as per your choice. The tasks are as
under:
1. Deposit Money
2. Withdraw Money
3. Check balance
0. Log Out
Write a menu driven program to take input from the user and perform the above
tasks.
The program checks the balance before withdrawal and finally displays the current
balance after transaction.
For an incorrect choice, an appropriate message should be displayed.

ANS:
import java.util.Scanner;

public class MyProgram


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 to deposit money");
System.out.println("Enter 2 to withdraw money");
System.out.println("Enter 3 to check balance");
System.out.println("Enter 0 to quit");

System.out.print("Enter your choice: ");


int ch = in.nextInt();
double amt = 0.0;
double bal = 5000.0; //Initial balance of account

switch(ch) {
case 0:
System.out.println("Thank you");
break;

case 1:
System.out.print("Enter deposit amount : ");
amt = in.nextDouble();
bal += amt;
System.out.println("Money deposited.");
System.out.print("Total balance = " + bal);
break;

case 2:
System.out.print("Enter withdrawal amount : ");
amt = in.nextDouble();
if(amt > bal) {
System.out.println("Insufficient balance.");
}
else {
bal -= amt;
System.out.println("Money withdrawn.");
System.out.print("Total balance = " + bal);
}
break;

case 3:
System.out.print("Total balance = " + bal);
break;

default:
System.out.println("Invalid request.");
}
}
}

You might also like