More Exercises

Exercise - Swap Numbers

כתבו תוכנית המחליפה את הערכים שלהם

דרך א

public class Main {
    public static void main(String[] args) {
        int num1 = MyConsole.readInt("enter 1st int: ");
        int num2 = MyConsole.readInt("enter 2nd int: ");

        int temp = num1;
        num1 = num2;
        num2 = temp;

        System.out.println("After swaping:" + " num1 = " + num1 + ", num2 = " + num2);
    }
}
enter 1st int:  100
enter 2nd int:  15
After swaping: num1 = 15, num2 = 100

דרך ב - בלי משתנה שלישי

public class Main {
    public static void main(String[] args) {
        int num1 = MyConsole.readInt("enter 1st int: ");
        int num2 = MyConsole.readInt("enter 2nd int: ");

        num1 = num1 + num2;
        num2 = num1 - num2;
        num1 = num1 - num2;
        System.out.println("After swaping:" + " num1 = " + num1 + ", num2 = " + num2);
    }
}
enter 1st int:  100
enter 2nd int:  15
After swaping: num1 = 15, num2 = 100

הסבר:

input: num1=100, num2=15

num1 = num1 + num2; //(num1: 115) = 100 + 15
num2 = num1 - num2; //(num2: 100) = 115 - 15
num1 = num1 - num2; //(num1: 15)  = 115 - 100

Exercise - Print Digits

כתוב תכנית

  • הקולטת מספר שלם תלת-ספרתי ומדפיסה את כל הספרות שלו בנפרד

  • מדפיסה את סכום הספרות שלו.

public class Main {
    public static void main(String[] args) {
        int num = MyConsole.readInt("enter 3-digit int: ");

        if (num>100 && num<1000){
            int singles = num % 10;
            int tens = num / 10 % 10;
            int hundreds = num / 100;

            System.out.println(hundreds);
            System.out.println(tens);
            System.out.println(singles);
            System.out.println("sum is: "+ (singles+tens+hundreds));
        } else {
            System.out.println("not valid number");
        }

    }
}
enter 3-digit int:  567
5
6
7
sum is: 18

מומלץ לשים כאן while על מנת שמהשמתש יכול להזין עוד מספר אם הוא לא בתווך

Exercise - Clock

כתוב תכנית שקולטת מספר שלם שמהווה מספר שניות והופכת אותו לשעות, דקות ושניות. ומדפיסה את ומדפיסה אותם. לדוגמה, מספר 3669 הופך שעה 1 ,דקה 1 ,9 שניות.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter seconds: ");
        int seconds_input = scanner.nextInt();

        int hours = seconds_input / 3600;
        int minutes = (seconds_input / 60) % 60 ;
        int seconds = seconds_input % 60;

        System.out.print("hours: " + hours);
        System.out.print(", minutes: " + minutes);
        System.out.print(", seconds: " + seconds + "\n");
    }
}
Enter seconds: 3669
hours: 1, minutes: 1, seconds: 9

Exercise - Roots of a quadratic equation

נוסחת השורשים

x=b±b24ac2ax = \frac{-b\pm\sqrt[]{b^2-4ac}}{2a}
determinant=b24acdeterminant = \sqrt[]{b^2-4ac}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        double secondRoot = 0, firstRoot = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the value of a: ");
        double a = sc.nextDouble();

        System.out.print("Enter the value of b: ");
        double b = sc.nextDouble();

        System.out.print("Enter the value of c: ");
        double c = sc.nextDouble();

        double determinant = (b*b)-(4*a*c);
        double sqrt = Math.sqrt(determinant);

        if(determinant>0){
            firstRoot = (-b + sqrt)/(2*a);
            secondRoot = (-b - sqrt)/(2*a);
            System.out.println("Roots are: "+ firstRoot +" and "+secondRoot);
        }else if(determinant == 0){
            System.out.println("Root is : "+(-b + sqrt)/(2*a));
        }
    }
}
Enter the value of a: 1
Enter the value of b: -1
Enter the value of c: -6

Roots are: 3.0 and -2.0
(x+2)(x3)=0x21x6=0(x + 2)(x - 3) = 0\\ x^2 -1x - 6 = 0

Exercise -Check if point is in radius of (x,y)

Hint: use Pythagorean theorem פיתגורס

Radius, diameter, & circumference | Circles (article) | Khan Academy
distance<radiusdistance < radius
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        //circle
        System.out.print("Enter x of circle: ");
        double x = sc.nextDouble();

        System.out.print("Enter y of circle: ");
        double y = sc.nextDouble();

        System.out.print("Enter radius: ");
        double radius = sc.nextDouble();

        //point
        System.out.print("Enter x of point: ");
        double x_point = sc.nextDouble();

        System.out.print("Enter y of point: ");
        double y_point = sc.nextDouble();

        //calculation
        double delta_x = x - x_point;
        double delta_y = y - y_point;
        double distance = Math.sqrt(Math.pow(delta_x, 2) + Math.pow(delta_y, 2));

        if (distance < radius)
            System.out.println("Point is inside circle");
        else if (distance == radius)
            System.out.println("Point is on the circles perimeter");
        else
            System.out.println("Point is outside circle");
    }
}
Enter x of circle: 6
Enter y of circle: 0
Enter radius: 5
Enter x of point: 3
Enter y of point: -4
Point is on the circles perimeter

another run

Enter x of circle: 0
Enter y of circle: 0
Enter radius: 5
Enter x of point: 5.1
Enter y of point: 0
Point is outside circle

Exercise - Rotate Square

Last updated