Restore
This commit is contained in:
10
Labs/Lab2/Lab2Part2_2230026071/Question1/.classpath
Normal file
10
Labs/Lab2/Lab2Part2_2230026071/Question1/.classpath
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
Labs/Lab2/Lab2Part2_2230026071/Question1/.project
Normal file
17
Labs/Lab2/Lab2Part2_2230026071/Question1/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question1</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
BIN
Labs/Lab2/Lab2Part2_2230026071/Question1/bin/SumAndAverage.class
Normal file
BIN
Labs/Lab2/Lab2Part2_2230026071/Question1/bin/SumAndAverage.class
Normal file
Binary file not shown.
@@ -0,0 +1,57 @@
|
||||
|
||||
public class SumAndAverage {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int sum = 0; // Store the accumulated sum init to 0
|
||||
double average; // average in double
|
||||
int lowerbound = 1; // The lowerbound to sum
|
||||
int upperbound = 100; // The upperbound to sum
|
||||
|
||||
int cntOdd = 0;
|
||||
// Use a for-loop to sum from lowerbound to upperbound
|
||||
for(int number = lowerbound; number <= upperbound; number+=2) { //only odd num: increment = 2
|
||||
sum += number; // same as "sum = sum + number"
|
||||
++cntOdd;
|
||||
}
|
||||
// Compute average in double. Beware that int/int produces int.
|
||||
average = (double) sum / cntOdd;
|
||||
// Print sum and average
|
||||
System.out.println("The sum is " + sum);
|
||||
System.out.println("The average is " + average);
|
||||
|
||||
// 6.1 while loop
|
||||
System.out.println("---- While-loop:");
|
||||
sum = 0;
|
||||
int number = lowerbound;
|
||||
int cnt_7 = 0;
|
||||
while(number <= upperbound) {
|
||||
if(number % 7 == 0) { //when can divided by 7, add sum and count
|
||||
sum += number;
|
||||
++cnt_7;
|
||||
}
|
||||
number++;
|
||||
}
|
||||
// Compute average in double. Beware that int/int produces int.
|
||||
average = (double) sum / cnt_7;
|
||||
// Print sum and average
|
||||
System.out.println("The sum is " + sum);
|
||||
System.out.println("The average is " + average);
|
||||
|
||||
// 6.2 do-while
|
||||
System.out.println("---- Do-while:");
|
||||
sum = 0; // Reset sum
|
||||
number = lowerbound;
|
||||
do {
|
||||
sum += number * number;
|
||||
number++;
|
||||
} while(number <= upperbound);
|
||||
// Compute average in double. Beware that int/int produces int.
|
||||
average = (double) sum / (upperbound - lowerbound + 1);
|
||||
// Print sum and average
|
||||
System.out.println("The sum is " + sum);
|
||||
System.out.println("The average is " + average);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user