Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package demo;
- // Basic exception handling
- import java.util.Scanner;
- public class HelloJava {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int choice = 0;
- do{
- printMenu();
- choice = input.nextInt();
- if(choice == 1){
- handleReverse();
- }else if(choice == 2){
- handleAverage();
- }else if(choice == 3){
- handleLinearEquation();
- }else if(choice != 4){
- System.out.println("Invalid choice");
- }
- }while(choice != 4);
- }
- public static void handleLinearEquation(){
- Scanner input = new Scanner(System.in);
- System.out.print("Enter a, b and c");
- try{
- int a = input.nextInt();
- int b = input.nextInt();
- int c = input.nextInt();
- double[] roots = getRoots(a, b, c);
- if(roots[0] == roots[1] && roots[0] != 0){
- System.out.println(roots[0]);
- }else if(roots[0] == 0 && roots[1] == 0){
- System.out.println("No solution");
- }else{
- System.out.println(roots[0] + ", " + roots[1]);
- }
- }catch(Exception e){
- System.out.println("a can not be zero");
- }
- }
- public static double[] getRoots(int a, int b, int c) throws Exception{
- if(a == 0){
- throw new Exception();
- }
- double[] roots = {0, 0};
- double D = b * b - 4 * a * c;
- if(D > 0){
- roots[0] = (-b + Math.sqrt(D)) / (2 * a);
- roots[1] = (-b - Math.sqrt(D)) / (2 * a);
- }else if(D == 0){
- roots[0] = -b / (2 * a);
- roots[1] = roots[0];
- }
- return roots;
- }
- public static void handleAverage(){
- Scanner input = new Scanner(System.in);
- System.out.print("Enter numbers separated by space: ");
- try{
- String line = input.nextLine();
- String[] numbers = line.split(" ");
- double average = getAverage(numbers);
- System.out.println("Average: " + average);
- }catch(Exception e){
- System.out.println("You must enter at least one number");
- }
- }
- public static double getAverage(String[] numbers) throws Exception {
- if(numbers.length <= 0){
- throw new Exception();
- }
- double sum = 0;
- double average = 0;
- for(int i = 0; i < numbers.length; i++){
- sum += Double.parseDouble(numbers[i]);
- }
- average = sum / numbers.length;
- return average;
- }
- public static void handleReverse(){
- Scanner input = new Scanner(System.in);
- System.out.print("Enter number: ");
- try{
- int number = input.nextInt();
- int reversed = reverse(number);
- System.out.println("Reversed: " + reversed);
- }catch (Exception e){
- System.out.println("Number must be positive");
- }
- }
- public static void printMenu(){
- System.out.println();
- System.out.println("1. Reverse number");
- System.out.println("2. Get average from sequence");
- System.out.println("3. Solve linear equation");
- System.out.println("4. Exit");
- }
- public static int reverse(int n) throws Exception{
- if(n < 0){
- throw new Exception();
- }
- int rev = 0;
- int length = String.valueOf(n).length();
- if(length == 1){
- rev = n;
- }else{
- while(n != 0){
- length--;
- int lastDigit = n % 10;
- rev += lastDigit * Math.pow(10, length);
- n /= 10;
- }
- }
- return rev;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement