-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL6Q6_AsciiDifference.java
65 lines (59 loc) · 1.7 KB
/
L6Q6_AsciiDifference.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/***
* 27-03-2020
*
* Lab 6: Question 6
*
* Description:
* Write a Java program, called AsciiDifference that contains a static method called difference()
* that accepts two Strings as the input parameters and returns the difference in the sum of ASCII
* values of the two Strings as an integer value. The input parameters should be read in from the
* user in the main method and passed to the difference() method. The main method should print
* returned integer to the screen.
*
* Sample Input: ireland
* IRELAND
*
*
* Sample Output: 224
*
* Explanation:
* String s1 = "ireland"; String s2 = "IRELAND";
* The sum of ASCII Values for s1 = 735 The sum of ASCII Values for s2 = 511
* The difference between s1 and s2 = 224
*
* Algorithm:
* Step 1: Two user input as two separate Strings
*
* Step 2: Pass Strings to difference() method
* A): Using a for-loop, convert each character of a String to its ascii value and sum the total
* B) Compute the difference between these sums
* C) Return the result to main
*
* Step 3: Print the result
*
***/
import java.util.Scanner;
public class L6Q6_AsciiDifference
{
public static void main(String[] args)
{ //Step 1:
Scanner scan = new Scanner(System.in);
String s1 = scan.nextLine();
String s2 = scan.nextLine();
scan.close();
System.out.println(difference(s1, s2)); //Step 2 & 3:
} //End main
public static int difference(String s1, String s2)
{
int x1 = 0, x2 = 0;
for(int i = 0; i < s1.length(); i++)
{ //A)
x1 += (int)s1.charAt(i);
}
for(int i = 0; i < s2.length(); i++)
{ //A)
x2 += (int)s2.charAt(i);
}
return (x1 - x2); //B) & C)
} //End difference()
} //End main