Explorar el Código

Implement Lab6 up to and including Exercise 4

Exercise 5 requries stdin so we will come abck to that once I figure out how
best to do that.

Primes generation could be more efficient.
main
Rob Hallam hace 1 año
padre
commit
4f8ba4d428
Se han modificado 1 ficheros con 135 adiciones y 1 borrados
  1. +135
    -1
      week1.java

+ 135
- 1
week1.java Ver fichero

@@ -1,10 +1,12 @@
import java.util.Calendar;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

class runLabs {
public static void main(String[] args) {
new Lab4();
new Lab5();
new Lab6();
}
}

@@ -13,11 +15,20 @@ class Section {

public Section(int s) {
this._sectionNumber = s;
this.printSection();
}

public int getSection() {
return _sectionNumber;
}

public void printSection() {
String title = String.format("Starting section %s", this._sectionNumber);

System.out.println("-".repeat(title.length()));
System.out.println(String.format("%s", title));
System.out.println("-".repeat(title.length()));
}
}

class Lab {
@@ -200,9 +211,132 @@ class Lab4 extends Lab {
}
}

/**
* Keyboard input
*/
class Lab5 extends Lab {
public Lab5() {
super(5);
System.out.println(String.format("hi"));
new five1();
}

class five1 extends Section {
/**
* We will now look at how we can get user input from the keyboard. We will
* gloss over some
* of the details here, but give you just the information you need to start
* getting input and
* processing it.
*/
public five1() {
super(1);
System.out.println(String.format("TBC"));
// Scanner keyboard = new Scanner(System.in);
// System.out.println("Please enter your name, followed by the return key?");
// String userEntry = keyboard.nextLine();
// System.out.println("Hello " + userEntry);
// System.out.println(String.format("%s", keyboard));
}
}
}

/**
* Loops
*/
class Lab6 extends Lab {
public Lab6() {
super(6);
new six1();
System.out.println(String.format("%n"));
new six3();
new six5();
}

/**
* Times tables
*/
class six1 extends Section {
public six1() {
super(1);
int testNum = 6;
System.out.println(String.format("Times table for %d:", testNum));
writeTimesTable(7);
}
}

class six3 extends Section {
public six3() {
super(3);
int testNum = 13; // statistically the most prime of all numbers
System.out.println(String.format("Testing primes..."));
System.out.println(String.format("%d prime? %s", testNum, isPrime(testNum)));
System.out.println(String.format("%d prime? %s", 23, isPrime(23)));
System.out.println(String.format("%d prime? %s", 27, isPrime(27)));
System.out.println(String.format("%d prime? %s", 28, isPrime(28)));
System.out.println(String.format("%d prime? %s", 299, isPrime(299)));
System.out.println(String.format("Generating primes..."));
generatePrimes(1024);
System.out.println(String.format("Done!%n"));
}
}

class six5 extends Section {
public six5() {
super(5);
System.out.println(String.format("TODO / NOT IMPLEMENTED until we get stdin working"));
}
}

public void writeTimesTable(int num, int limit) {
for (int i = 0; i < (limit + 1); i++) {
System.out.printf("%d\t", num * i);
}
}

public void writeTimesTable(int num) {
int DEFAULT_LIMIT = 10;
writeTimesTable(num, DEFAULT_LIMIT);
}

public void generatePrimes(int upperLimit) {
// handle 2 as a special case
System.out.println(String.format("2"));
// for (int i = 3; i <= upperLimit; i += 2) {
for (int i = 2; i <= upperLimit; i++) {
// TODO: figure out smarter way of doing this, I'm sure primes must be at least 6 apart...
// Something about 2n ± 1 ?
// seive of Erasthotenes ..?
if (isPrime(i)) {
System.out.printf("%d\t", i);
}
}
}

/**
* But is it prime?
* @param num Integer to test for primality
* @return if it's prime
*/
public boolean isPrime(int num) {
boolean DEBUG = false;
if ((num == 2) || (num == 1)) {
return true;
}
if (num % 2 == 0){
return false;
} else {
int divisor = 3;
double numSquareRoot = Math.sqrt((double)num);
while (divisor <= numSquareRoot) {
if (num % divisor == 0) {
return false;
}
divisor += 2;
if (DEBUG) {
System.out.printf("%d\t", divisor);
}
}
return true;
}
}
}

Cargando…
Cancelar
Guardar