-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignmentFour
More file actions
88 lines (82 loc) · 2.22 KB
/
AssignmentFour
File metadata and controls
88 lines (82 loc) · 2.22 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
*
*/
/**
* @author Kumin
*
*/
import java.util.Scanner;
import java.math.*;
import java.io.File;
import java.io.FileNotFoundException;
public class AssignmentFour {
/**
* @param args
*/
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Kumin In, CECS 174, Assignment Four");
Scanner sc = null;
int counter = 0;
double sum = 0, min = 0, max = 0;
boolean firstTime = true;
try {
sc = new Scanner(new File(args[0]));
while (sc.hasNextDouble()) {
double x = sc.nextDouble();
if (firstTime == true) {
max = x;
min = x;
firstTime = false;
} else if (min > x) {
min = x;
} else if (max < x) {
max = x;
}
sum = sum + x;
counter++;
FahrenheightToCelsiusConverter(x);
CelsiusToFahrenheightConverter(x);
NewtonRaphsonMethod(x);
System.out.print("Sqrt of " + x + " by math.sqrt is "
+ Math.sqrt(x) + "\n\n");
}
SumAndAverage(sum, counter);
System.out.print("Minimum is: " + min + "\nMaximum is: " + max);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
}
public static void CelsiusToFahrenheightConverter(double Celsius) {
double Fahrenheight = ((Celsius * 9) / 5) + 32;
System.out.print(Celsius + " degrees Celsius = " + Fahrenheight
+ " degrees FahrenHeight\n");
}
public static void FahrenheightToCelsiusConverter(double Fahrenheight) {
double Celsius = ((Fahrenheight - 32) * 5) / 9;
System.out.print(Fahrenheight + " degrees Fahrenhieght = " + Celsius
+ " degrees Celsius\n");
}
public static void NewtonRaphsonMethod(double x) {
double epsilon = 1e-10;
double x1 = x;
int counter = 0;
if (x > 0) {
while ((x1 - (x / x1) > (epsilon * x1))) {
x1 = (x / x1 + x1) / 2.0;
counter++;
}
System.out.println("Sqrt of " + x + " by Newton/Raphson is " + x1
+ " after " + counter + " iterations.");
}
if (x < 0) {
System.out.println("Sqrt of " + x + " is not a real number");
}
}
public static void SumAndAverage(double sum, int counter) {
double average = sum / counter;
System.out.println("Sum is " + sum + ", count is " + counter
+ ", average is " + average);
}
}