Java101
  • Welcome
  • Videos 🖥️
    • Video Links
  • Exercises
    • Assignment 1 - Tests - הבודק
    • Exercise 2
    • Exercise 4
  • Lesson 1 - Intro
    • MyConsole
    • Variables, Conditionals and Exercises
    • More Exercises
    • IDE Shortcuts
  • Lesson 2
    • Lesson 2 - While
  • Lesson 3 - for loops
    • for loops
  • Lesson 4 - Arrays
    • Arrays
  • Lesson 5 - Functions and Multi-dim Arrays
    • Multidimensional arrays & Functions
  • Lesson 6 - Strings, JUnit and Recursion
    • Useful String functions and more
    • Strings
    • More Exercises
  • Lesson 7
    • Recursion
    • More Exercises
  • Lesson 8 - OOP
    • Intro to Classes and Objects
    • Exercise: Creating circles
    • Lesson 9 - More OOP
  • Lesson 9 - Linked Lists
    • Linked Lists
  • Lesson 10 - Interfaces
    • Interface - Intro
    • Interfaces - Exercises
  • Lesson 11 - Test Review
    • index
Powered by GitBook
On this page
  • Exercise - Swap Numbers
  • דרך א
  • דרך ב - בלי משתנה שלישי
  • Exercise - Print Digits
  • Exercise - Clock
  • Exercise - Roots of a quadratic equation
  • Exercise -Check if point is in radius of (x,y)
  • Exercise - Rotate Square

Was this helpful?

  1. Lesson 1 - Intro

More Exercises

PreviousVariables, Conditionals and ExercisesNextIDE Shortcuts

Last updated 4 years ago

Was this helpful?

Exercise - Swap Numbers

Arrow Swap Icons - Download Free Vector Icons | Noun Project

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

דרך א

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

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

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

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

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

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

Numeral - Math Definitions - Letter N
Magis Tempo Wall Clock by Lumens - Dwell
Graphs of Quadratic Functions | Boundless Algebra
x=−b±b2−4ac2ax = \frac{-b\pm\sqrt[]{b^2-4ac}}{2a}x=2a−b±b2−4ac​​
determinant=b2−4acdeterminant = \sqrt[]{b^2-4ac}determinant=b2−4ac​
(x+2)(x−3)=0x2−1x−6=0(x + 2)(x - 3) = 0\\ x^2 -1x - 6 = 0(x+2)(x−3)=0x2−1x−6=0
Radius, diameter, & circumference | Circles (article) | Khan Academy
distance
distance<radiusdistance < radiusdistance<radius