1 / 16

Important 10 programmes to deal with Java

provides a valuable idea of few programmes used in JAVA this gives a clear idea about some of the important concepts like array and many more. these questions are mostly from the ISC board exam question papers.

Suresh141
Download Presentation

Important 10 programmes to deal with Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Question 1 Define a class called Mobike with the following specifications: Data Members 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 void input() To input and store the details of the customer void compute() To compute the rental charge. 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 void display() To display the details in the given format Name of the customer: ........................... Number of days: ........................... Bill amount: ........................... Write a main method to create an object of the class and call the above member methods ——————————————————————————————————— import java.util.Scanner; class Mobike { // Data members int bno; int phno; String name; int days; int charge; // Method to input details void input()

  2. { Scanner sc = new Scanner(System.in); System.out.print("Enter bike number: "); bno = sc.nextInt(); System.out.print("Enter phone number: "); phno = sc.nextInt(); sc.nextLine(); // to clear buffer System.out.print("Enter customer name: "); name = sc.nextLine(); System.out.print("Enter number of days: "); days = sc.nextInt(); } // Method to compute rental charge void compute() { if (days <= 5) { charge = days * 500; } else if (days <= 10) { charge = (5 * 500) + (days - 5) * 400; } else { charge = (5 * 500) + (5 * 400) + (days - 10) * 200; } } // Method to display details void display() { System.out.println("\nName of the customer: " + name); System.out.println("Number of days: " + days); System.out.println("Bill amount: ₹" + charge);

  3. } // Main method public static void main(String args[]) { Mobike mb = new Mobike(); mb.input(); mb.compute(); mb.display(); } } ————————————————————————————————. Question 2 Define a class ElectricBill with the following specifications: Class name : ElectricBill Data members: String n — to store the name of the customer int units — to store the number of units consumed double bill — to store the amount to be paid Member methods: void accept( ) — to accept the name of the customer and number of units consumed void calculate( ) — to calculate the bill as per the following tariff: Number of units Rate per unit First 100 units Nil Next 200 units Rs.3.00 Above 300 units Rs.5.00 A surcharge of 2.5% charged if the number of units consumed is above 300 units. void print( ) — To print the details as follows: Name Units Consumed Bill amount ........................... ........................... ...........................

  4. Write a main method to create an object of the class and call the above member Methods. ———————————————————————————————————— import java.util.Scanner; class ElectricBill { String n; int units; double bill; void accept() { Scanner sc = new Scanner(System.in); System.out.print("Enter name: "); n = sc.nextLine(); System.out.print("Enter units consumed: "); units = sc.nextInt(); } void calculate() { if (units <= 100) bill = 0; else if (units <= 300) bill = (units - 100) * 3; else { bill = (200 * 3) + (units - 300) * 5; bill = bill + bill * 0.025; } } void print() { System.out.println("Name\tUnits Consumed\tBill Amount"); System.out.println(n + "\t" + units + "\t\tRs." + bill); } public static void main(String args[]) { ElectricBill eb = new ElectricBill(); eb.accept(); eb.calculate(); eb.print(); } }

  5. ———————————————————————————————————————————————————————————————————————— Question 3 Anshul transport company charges for the parcels of its customers as per the following specifications given below: Class name: Atransport Member variables: String name – to store the name of the customer int w – to store the weight of the parcel in Kg int charge – to store the charge of the parcel Member functions: void accept ( ) – to accept the name of the customer, weight of the parcel from the user (using Scanner class) void calculate ( ) – to calculate the charge as per the weight of the parcel as per the following criteria: Weight in Kg Charge per Kg Upto 10 Kgs Rs.25 per Kg Next 20 Kgs Rs.20 per Kg Above 30 Kgs Rs.10 per Kg A surcharge of 5% is charged on the bill. void print ( ) – to print the name of the customer, weight of the parcel, total bill inclusive of surcharge. Define a class with the above-mentioned specifications, create the main method, create an object

  6. and invoke the member methods ———————————————————————————————————— —————————— import java.util.Scanner; class Atransport { String name; int w; double charge; void accept() { Scanner sc = new Scanner(System.in); System.out.print("Enter customer name: "); name = sc.nextLine(); System.out.print("Enter weight: of the parcell "); w = sc.nextInt(); } void calculate() { if (w <= 10) charge = w * 25; else if (w <= 30) charge = (10 * 25) + (w - 10) * 20; else charge = (10 * 25) + (20 * 20) + (w - 30) * 10; charge = charge + charge * 0.05; } void print() { System.out.println("Name: " + name); System.out.println("Weight: " + w); System.out.println("Total Charge: Rs." + charge); } public static void main(String args[]) { Atransport at = new Atransport(); at.accept(); at.calculate(); at.print();

  7. } } ———————————————————————————————————— —————————— Question 4: Define a class named BookFair with the following description: Instance variables/data members: String bName – stores the name of the book double price – stores the price of the book Member functions: void input() – to input and store the name and the price of the book void calculate() – to calculate the price after discount. Discount is calculated based on the following criteria: Price Discount Less than or equal to ₹ 1000 2% of price More than ₹ 1000 and less than or equal to ₹ 3000 10% of price More than ₹ 3000 15% of price void display() – to display the name and price of the book after discount. Write a main() method to create an object of the class and call the above methods. ———————————————————————————————————— —————————— import java.util.Scanner; class BookFair { String bName;

  8. double price; void input() { Scanner sc = new Scanner(System.in); System.out.print("Enter book name: "); bName = sc.nextLine(); System.out.print("Enter price: "); price = sc.nextDouble(); } void calculate() { if (price <= 1000) price -= price * 0.02; else if (price <= 3000) price -= price * 0.10; else price -= price * 0.15; } void display() { System.out.println("Book Name: " + bName); System.out.println("Price after discount: Rs." + price); }

  9. public static void main(String args[]) { BookFair bf = new BookFair(); bf.input(); bf.calculate(); bf.display(); } } ———————————————————————————————————— ————————— Question 5: Write a java program to print the Fibonacci Series. ———————————————————————————————————— —————————— class Fibonacci { public static void main(String args[]) { int a = 0, b = 1, c; System.out.print(a + " " + b + " "); for (int i = 1; i <= 8; i++) { c = a + b; System.out.print(c + " "); a = b; b = c; } }}

  10. ———————————————————————————————————————————————————————————————————————— —————————— Question 6: Write a menu driven program to print the following patterns. 1. To print Floyd’s triangle 1 2 3 4 5 6 7 8 9 10 2. To print the given pattern @ # @ # @ # @ @ # @ ———————————————————————————————————— ——————— import java.util.Scanner; class Patterns { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("1. Floyd's Triangle or 2. @ # Pattern"); int ch = sc.nextInt(); switch (ch) { case 1: int num = 1; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) System.out.print(num++ + " "); System.out.println();

  11. } break; case 2: for (int i = 4; i >= 1; i--) { for (int j = 1; j <= i; j++) System.out.print((j % 2 == 1) ? "@ " : "# "); System.out.println(); } break; default: System.out.println("Invalid choice"); } } } ————————————————————————————————— —————————— Question 7: Write a program to input a number and verify whether it is a Lead Number or not. A number is called a Lead number if the sum of even digits is equal to the sum of the odd digits. ————————————————————————————————— —————————— import java.util.Scanner; class LeadNumber { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n, sEven = 0, sOdd = 0; System.out.println("Enter a number"); n = sc.nextInt(); while (n > 0) { int d = n % 10; if (d % 2 == 0) sEven += d; else sOdd += d;

  12. n /= 10; } if (sEven == sOdd) System.out.println("Lead Number"); else System.out.println("Not a Lead Number"); } } ————————————————————————————————— —————————— Question 8: Write a program to accept a number and check whether the number is perfect number or not. A number is called perfect number, if the sum of all factors(except number itself) of the number is equal to that number. (For eg 6 is a perfect number. Factors are 1,2 & 3 and the sum is 1+2+3=6). ————————————————————————————————— —————————— import java.util.Scanner; class PerfectNumber { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n,sum = 0; System.out.println("Enter a number to check"); n = sc.nextInt(); for (int i = 1; i < n; i++) if (n % i == 0) sum += i; if (sum == n) System.out.println("Perfect Number"); else System.out.println("Not a Perfect Number"); } } ————————————————————————————————— —————————— Using the switch statement, write a menu-driven program for the following: (i) To print the given pattern

  13. A A B A B C A B C D (ii) To display the sum of the series given below: ? − ? = 1 ? + 2 ? − 3 ? + 4 ? … … … ?????? ————————————————————————————————— —————————— import java.util.Scanner; class MenuProgram { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("1. Alphabet Pattern"); System.out.println("2. Series Sum"); int ch = sc.nextInt(); switch (ch) { case 1: for (char i = 'A'; i <= 'D'; i++) { for (char j = 'A'; j <= i; j++) System.out.print(j + " "); System.out.println(); } 5 For an incorrect option, an appropriate error message should be displayed.

  14. break; case 2: System.out.print("Enter x and n: "); int x = sc.nextInt(); int n = sc.nextInt(); double sum = 0; for (int i = 1; i <= n; i++) sum += Math.pow(-1, i + 1) * Math.pow(x, i); System.out.println("Sum = " + sum); break; default: System.out.println("Invalid option"); } } } ————————————————————————————————— —————————— Write a program to input a number and check and print whether it is an EvenPal number or not. Note: The number is said to be an EvenPal number when the number is a palindrome number and the sum of its digits is an even number. Example: Number 121 is a palindrome number and the sum of its digits is 1 + 2 + 1 = 4, which is an even number. Therefore, 121 is an EvenPal number. ————————————————————————————————— —————————— import java.util.Scanner; class EvenPal { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n, temp, rev = 0, sum = 0; System.out.println("Enter a number to check"); n = sc.nextInt();

  15. temp=n; while (temp > 0) { int d = temp % 10; rev = rev * 10 + d; sum += d; temp /= 10; } if (rev == n && sum % 2 == 0) System.out.println("EvenPal Number"); else System.out.println("Not an EvenPal Number"); } } ————————————————————————————————— ——————————

More Related