Commit fe9c5978 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Loop + rounding source code

parent 2219196b
package de.hdm_stuttgart.de.sd1.loop;
public class LoopExample {
public static void main(String [] args) {
final int upperLimit = 14;
while_squareNumbers(upperLimit);
doWhile_squareNumbers(upperLimit);
}
/**
* Compute all square numbers starting from 4 in steps of 3
* being smaller than a given limit. The results are being written to standard output.
* Implemented by a while -loop.
*
* @param limit An upper exclusive bound for the highest number to be squared..
*/
public static void while_squareNumbers(final int limit) {
System.out.println("Computing square numbers");
int i = 4;
while (i < limit) {
System.out.println("i = " + i + ", i * i = " + i * i);
i += 3;
}
System.out.println("Finished computing square numbers");
}
/**
* Compute all square numbers starting from 4 in steps of 3
* being smaller than a given limit. The results are being written to standard output.
* Implemented by a do ... while -loop.
*
* @param limit An upper exclusive bound for the highest number to be squared..
*/
public static void doWhile_squareNumbers(final int limit) {
System.out.println("Computing square numbers");
int i = 4;
if (i < limit) { // Needed !!!
do {
System.out.println("i = " + i + ", i * i = " + i * i);
i += 3;
} while (i < limit);
}
System.out.println("Finished computing square numbers");
}
}
package de.hdm_stuttgart.de.sd1.loop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
package de.hdm_stuttgart.de.sd1.loop;
public class LoopSolve {
public static void main(String [] args) {
squareNumbers(14);
}
/**
* Compute all square numbers starting from 4 in steps of 3
* being smaller than a given limit. The results are being written to standard output.
* Implemented by a for -loop.
*
* @param limit An upper exclusive bound for the highest number to be squared..
*/
public static void squareNumbers(final int limit) {
System.out.println("Computing square numbers");
for (int i = 4; i < limit; i += 3) {
System.out.println("i = " + i + ", i * i = " + i * i);
}
System.out.println("Finished computing square numbers");
}
}
package de.hdm_stuttgart.de.sd1.loop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hdm-stuttgart.de.sd1</groupId>
<artifactId>loop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>loop</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javadocDestdir>~/tmp</javadocDestdir>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<linksource>true</linksource>
<links>
<link>http://www.greenfoot.org/files/javadoc/</link>
</links>
<!-- Setting destDir interferes with taglet class -->
<additionalJOptions>
<additionalJOption>-d ${javadocDestdir}</additionalJOption>
</additionalJOptions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package de.hdm_stuttgart.de.sd1.rounding;
/**
* Hello world!
*
*/
public class MathTable
{
public static void main( String[] args )
{
for (int i = 0; i < 360; i += 5) {
double sinValue = Math.sin(i * Math.PI / 180);
System.out.println(i + ": " + Math.round(100 * sinValue));
}
}
}
package de.hdm_stuttgart.de.sd1.rounding;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment