Skip to content

Commit

Permalink
Added Task 1 Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohaned Yossry Abdulaziz committed Nov 10, 2020
1 parent a96d95d commit fe2a815
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Task1Solution/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Task1Solution/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Task1Solution/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Task1Solution/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Task1Solution/Task1Solution.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name:
-------------------------------------
Group:
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name:
-------------------------------------
Group:
Binary file not shown.
128 changes: 128 additions & 0 deletions Task1Solution/src/com/task1/Task1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.task1;


import java.util.Scanner;

/*
TODO Task Hint:
copy this line to be able to define Scanner object to Take input from user
Scanner input = new Scanner(System.in);
*/
public class Task1 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Number: ");
long num = input.nextLong();
long result = sumDigits(num);
System.out.println(result);
//=================================================================//
System.out.println("Enter First Number: ");
double num1 = input.nextDouble();
System.out.println("Enter Second Number: ");
double num2 = input.nextDouble();
System.out.println("Enter Third Number: ");
double num3 = input.nextDouble();
displaySortedNumbers(num1, num2, num3);
//=================================================================//
calcScore();
//=================================================================//

}

/*
TODO: Question 1: (Sum the digits in an integer) Write a method that computes the sum of the digits in an long.
* Use the following method header:
public static long sumDigits(long n)
---------------------------------------------
* Explanation:
For example, sumDigits(234) returns 9 (= 2 + 3 + 4).
(Hint: Use the % operator to extract digits and the / operator to remove the extracted digit. For instance,
to extract 4 from 234, use 234 % 10 (= 4 ). To remove 4 from 234, use 234 / 10
(= 2 3 ). Use a loop to repeatedly extract and remove the digit until all the digits
are extracted.
> Bonus : Learn more about Math.abs() method and try to use it in the task through it's documentation
----------------------------------------------
* Write a method that prompts the user to enter an long then displays the sum of all its digits and call
this method in main
method to test the program.
*/

//TODO Write Answer for Question 1 Here
public static long sumDigits(long n) {
//n=>234
long sum = 0;
n = Math.abs(n);
while (n != 0) {
// digit => 4 => 3 => 2
long digit = n % 10;
// 0 => 4 => 7 => 9
sum += digit;
//234=>23 => 2 => 0
n = n / 10;
}
return sum;
}

//-----------------------------------------------------------------------//

/*
TODO: Question 2: (Sort three numbers) Write a method with the following header to display three
numbers in decreasing order:
---------------------
public static void displaySortedNumbers(double num1, double num2, double num3)
---------------------
Write a test program that prompts the user to enter three numbers and invokes the
method to display them in decreasing order.
*/

//TODO Write Answer for Question 2 Here
public static void displaySortedNumbers(double max, double mid, double low) {
double temp;
if (mid >= max && mid >= low) {
temp = max;
max = mid;
mid = temp;
} else if (low >= max && low >= mid) {
temp = max;
max = low;
low = temp;
}
if (low > mid) {
temp = low;
low = mid;
mid = temp;
}


System.out.println(max + ", " + mid + ", " + low);

}

//-----------------------------------------------------------------------//

/*
TODO: Question 3: Write a method that prompts a student to enter a score.
If the score is greater or equal to 60, display “you pass the exam”;
otherwise, display “you don’t pass the exam”. Your program ends with input -1.
*/

//TODO Write Answer for Question 3 Here
public static void calcScore() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter score");
double score = scanner.nextDouble();
while (score != -1) {
if (score >= 60)
System.out.println("You pass the exam");
else
System.out.println("You don't pass the exam");
System.out.println("Enter score again");
score = scanner.nextDouble();
}
}

//-----------------------------------------------------------------------//

}
3 changes: 3 additions & 0 deletions Task1Solution/src/com/task1/student_info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name:
-------------------------------------
Group:

0 comments on commit fe2a815

Please sign in to comment.