├── task 2.java ├── task 1.java └── task 3.java /task 2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class GradeCalculator { 4 | 5 | public static void main(String[] args) { 6 | Scanner scanner = new Scanner(System.in); 7 | 8 | System.out.println("Welcome to the Grade Calculator!"); 9 | 10 | // Input: Take marks obtained in each subject 11 | System.out.print("Enter the number of subjects: "); 12 | int numberOfSubjects = scanner.nextInt(); 13 | 14 | int totalMarks = 0; 15 | 16 | for (int i = 1; i <= numberOfSubjects; i++) { 17 | System.out.print("Enter marks obtained in subject " + i + ": "); 18 | int subjectMarks = scanner.nextInt(); 19 | totalMarks += subjectMarks; 20 | } 21 | 22 | // Calculate Total Marks 23 | System.out.println("Total Marks: " + totalMarks); 24 | 25 | // Calculate Average Percentage 26 | double averagePercentage = (double) totalMarks / numberOfSubjects; 27 | System.out.println("Average Percentage: " + averagePercentage + "%"); 28 | 29 | // Grade Calculation 30 | char grade; 31 | 32 | if (averagePercentage >= 90) { 33 | grade = 'A'; 34 | } else if (averagePercentage >= 80) { 35 | grade = 'B'; 36 | } else if (averagePercentage >= 70) { 37 | grade = 'C'; 38 | } else if (averagePercentage >= 60) { 39 | grade = 'D'; 40 | } else { 41 | grade = 'F'; 42 | } 43 | 44 | // Display Results 45 | System.out.println("Grade: " + grade); 46 | 47 | scanner.close(); 48 | } 49 | } -------------------------------------------------------------------------------- /task 1.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | import java.util.Scanner; 3 | 4 | public class GuessTheNumberGame { 5 | 6 | public static void main(String[] args) { 7 | Scanner scanner = new Scanner(System.in); 8 | Random random = new Random(); 9 | int minRange = 1; 10 | int maxRange = 100; 11 | int maxAttempts = 10; 12 | int score = 0; 13 | 14 | System.out.println("Welcome to the Guess the Number Game!"); 15 | 16 | do { 17 | int targetNumber = random.nextInt(maxRange - minRange + 1) + minRange; 18 | System.out.println("I have selected a number between " + minRange + " and " + maxRange + ". Guess it!"); 19 | 20 | for (int attempt = 1; attempt <= maxAttempts; attempt++) { 21 | System.out.print("Attempt " + attempt + ": Enter your guess: "); 22 | int userGuess = scanner.nextInt(); 23 | 24 | if (userGuess == targetNumber) { 25 | System.out.println("Congratulations! You guessed the correct number."); 26 | score += maxAttempts - (attempt - 1); 27 | break; 28 | } else if (userGuess < targetNumber) { 29 | System.out.println("Too low. Try again."); 30 | } else { 31 | System.out.println("Too high. Try again."); 32 | } 33 | 34 | if (attempt == maxAttempts) { 35 | System.out.println("Sorry, you've run out of attempts. The correct number was: " + targetNumber); 36 | } 37 | } 38 | 39 | System.out.print("Do you want to play again? (yes/no): "); 40 | } while (scanner.next().equalsIgnoreCase("yes")); 41 | 42 | System.out.println("Your final score is: " + score); 43 | System.out.println("Thanks for playing! Goodbye."); 44 | scanner.close(); 45 | } 46 | } -------------------------------------------------------------------------------- /task 3.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class BankAccount { 4 | private double balance; 5 | 6 | public BankAccount(double initialBalance) { 7 | this.balance = initialBalance; 8 | } 9 | 10 | public double getBalance() { 11 | return balance; 12 | } 13 | 14 | public void deposit(double amount) { 15 | balance += amount; 16 | } 17 | 18 | public boolean withdraw(double amount) { 19 | if (amount > balance) { 20 | System.out.println("Insufficient funds. Withdrawal failed."); 21 | return false; 22 | } else { 23 | balance -= amount; 24 | System.out.println("Withdrawal successful. Remaining balance: " + balance); 25 | return true; 26 | } 27 | } 28 | } 29 | 30 | class ATM { 31 | private BankAccount bankAccount; 32 | 33 | public ATM(BankAccount bankAccount) { 34 | this.bankAccount = bankAccount; 35 | } 36 | 37 | public void displayMenu() { 38 | System.out.println("ATM Menu:"); 39 | System.out.println("1. Check Balance"); 40 | System.out.println("2. Deposit"); 41 | System.out.println("3. Withdraw"); 42 | System.out.println("4. Exit"); 43 | } 44 | 45 | public void performTransaction(int choice, Scanner scanner) { 46 | switch (choice) { 47 | case 1: 48 | System.out.println("Current Balance: " + bankAccount.getBalance()); 49 | break; 50 | case 2: 51 | System.out.print("Enter deposit amount: "); 52 | double depositAmount = scanner.nextDouble(); 53 | bankAccount.deposit(depositAmount); 54 | System.out.println("Deposit successful. New balance: " + bankAccount.getBalance()); 55 | break; 56 | case 3: 57 | System.out.print("Enter withdrawal amount: "); 58 | double withdrawalAmount = scanner.nextDouble(); 59 | bankAccount.withdraw(withdrawalAmount); 60 | break; 61 | case 4: 62 | System.out.println("Exiting ATM. Thank you!"); 63 | System.exit(0); 64 | break; 65 | default: 66 | System.out.println("Invalid choice. Please select a valid option."); 67 | } 68 | } 69 | } 70 | 71 | public class Main { 72 | public static void main(String[] args) { 73 | Scanner scanner = new Scanner(System.in); 74 | 75 | System.out.print("Enter initial balance: "); 76 | double initialBalance = scanner.nextDouble(); 77 | 78 | BankAccount userAccount = new BankAccount(initialBalance); 79 | ATM atmMachine = new ATM(userAccount); 80 | 81 | while (true) { 82 | atmMachine.displayMenu(); 83 | System.out.print("Enter your choice (1-4): "); 84 | int choice = scanner.nextInt(); 85 | 86 | atmMachine.performTransaction(choice, scanner); 87 | } 88 | } 89 | } --------------------------------------------------------------------------------