add assignment 2 q1 & q2
This commit is contained in:
8 files changed
+100548
No files matched your search
+100222
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,81 @@
|
||||
package ca.bcit.comp1510.assignment2.q1;
|
||||
|
||||
/**
|
||||
* Cylinder data class.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Cylinder {
|
||||
|
||||
/** radius double. **/
|
||||
private double radius;
|
||||
|
||||
/** height double. **/
|
||||
private double height;
|
||||
|
||||
/**
|
||||
* Cylinder constructor.
|
||||
* @param r double radius
|
||||
* @param h double height
|
||||
* */
|
||||
public Cylinder(double r, double h) {
|
||||
radius = r;
|
||||
height = h;
|
||||
}
|
||||
|
||||
/**
|
||||
* getRaidus.
|
||||
* @return double radius
|
||||
*/
|
||||
public double getRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* getHeight.
|
||||
* @return double height
|
||||
*/
|
||||
public double getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
/**
|
||||
* setRadius.
|
||||
* @param r double new radius
|
||||
*/
|
||||
public void setRadius(double r) {
|
||||
radius = r;
|
||||
}
|
||||
|
||||
/**
|
||||
* setHeight.
|
||||
* @param h double new height
|
||||
*/
|
||||
public void setHeight(double h) {
|
||||
height = h;
|
||||
}
|
||||
|
||||
/**
|
||||
* volume.
|
||||
* @return double cylinder volume
|
||||
*/
|
||||
public double volume() {
|
||||
return Math.PI * radius * radius * height;
|
||||
}
|
||||
|
||||
/**
|
||||
* surfaceArea.
|
||||
* @return double cylinder surface area
|
||||
*/
|
||||
public double surfaceArea() {
|
||||
return 2 * Math.PI * radius * (radius + height);
|
||||
}
|
||||
|
||||
/**
|
||||
* toString.
|
||||
* @return string representation of cylinder
|
||||
*/
|
||||
public String toString() {
|
||||
return "Radius: " + radius + "\nHeight: " + height;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package ca.bcit.comp1510.assignment2.q1;
|
||||
|
||||
/**
|
||||
* <p>This is where you put your description about what this class does. You
|
||||
* don't have to write an essay but you should describe exactly what it does.
|
||||
* Describing it will help you to understand the programming problem better.</p>
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class MultiCylinder {
|
||||
/**
|
||||
* <p>This is the main method (entry point) that gets called by the JVM.</p>
|
||||
*
|
||||
* @param args command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
final int r1 = 4;
|
||||
final int r2 = 2;
|
||||
final int r3 = 6;
|
||||
final int r4 = 3;
|
||||
final int h1 = 6;
|
||||
final int h2 = 5;
|
||||
final int h3 = 10;
|
||||
final int h4 = 8;
|
||||
|
||||
|
||||
Cylinder c1 = new Cylinder(r1, h1);
|
||||
Cylinder c2 = new Cylinder(r2, h2);
|
||||
|
||||
System.out.println(c1.toString());
|
||||
System.out.println(c2.toString());
|
||||
|
||||
c1.setRadius(r3);
|
||||
c1.setHeight(h3);
|
||||
c2.setRadius(r4);
|
||||
c2.setHeight(h4);
|
||||
|
||||
System.out.println(c1.toString());
|
||||
System.out.println(c2.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package ca.bcit.comp1510.assignment2.q2;
|
||||
|
||||
/**
|
||||
* Word not like Microsoft word.
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class Word implements Comparable<Word> {
|
||||
|
||||
/** word. */
|
||||
private final String word;
|
||||
|
||||
/** frequency of word. */
|
||||
private int frequency = 1;
|
||||
|
||||
/**
|
||||
* Word constructor.
|
||||
* @param w new word.
|
||||
* */
|
||||
public Word(String w) {
|
||||
word = w;
|
||||
}
|
||||
|
||||
/**
|
||||
* getWord.
|
||||
* @return string word
|
||||
*/
|
||||
public String getWord() {
|
||||
return word;
|
||||
}
|
||||
|
||||
/**
|
||||
* getFrequency.
|
||||
* @return int frequency
|
||||
*/
|
||||
public int getFrequency() {
|
||||
return frequency;
|
||||
}
|
||||
|
||||
/** increment frequency. */
|
||||
public void increment() {
|
||||
frequency++;
|
||||
}
|
||||
|
||||
/**
|
||||
* toString.
|
||||
* @return string representation.
|
||||
*/
|
||||
public String toString() {
|
||||
return word + " " + frequency;
|
||||
}
|
||||
|
||||
/**
|
||||
* compareTo another Word class.
|
||||
* @param w Word object
|
||||
* @return larger frequency
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(Word w) {
|
||||
return Integer.compare(w.getFrequency(), frequency);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package ca.bcit.comp1510.assignment2.q2;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* <p>This is where you put your description about what this class does. You
|
||||
* don't have to write an essay but you should describe exactly what it does.
|
||||
* Describing it will help you to understand the programming problem better.</p>
|
||||
*
|
||||
* @author Braeden Sowinski
|
||||
* @version 1.0.0
|
||||
*/
|
||||
public class WordCounter {
|
||||
|
||||
/** words list. */
|
||||
private List<Word> words = new ArrayList<Word>();
|
||||
|
||||
/**
|
||||
* parseBook returns the number of unique words found in a given
|
||||
* book.
|
||||
* @param filename string.
|
||||
* @return numberOfWords int.
|
||||
*/
|
||||
public int parseBook(String filename) throws FileNotFoundException {
|
||||
|
||||
Scanner fileScan = new Scanner(new File(filename));
|
||||
Scanner lineScan;
|
||||
|
||||
while (fileScan.hasNext()) {
|
||||
lineScan = new Scanner(fileScan.nextLine());
|
||||
lineScan.useDelimiter(" ");
|
||||
|
||||
while (lineScan.hasNext()) {
|
||||
String word = lineScan.next().toLowerCase();
|
||||
|
||||
boolean exists = false;
|
||||
for (int i = 0; i < words.size() && !exists; i++) {
|
||||
if (words.get(i).getWord().equals(word)) {
|
||||
words.get(i).increment();
|
||||
// look I did not use a break statement...
|
||||
exists = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!exists) {
|
||||
words.add(new Word(word));
|
||||
}
|
||||
}
|
||||
lineScan.close();
|
||||
}
|
||||
|
||||
fileScan.close();
|
||||
|
||||
return words.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* printTopWords.
|
||||
* @param n int number of words.
|
||||
*/
|
||||
public void printTopWords(int n) {
|
||||
words.sort(null);
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.println(words.get(i).toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>This is the main method (entry point) that gets called by the JVM.</p>
|
||||
*
|
||||
* @param args command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
WordCounter counter = new WordCounter();
|
||||
|
||||
int numberOfWords;
|
||||
|
||||
try {
|
||||
numberOfWords = counter.parseBook("src/bible.txt");
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("Error: File not found");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println(numberOfWords + " words\n");
|
||||
|
||||
final int top10 = 10;
|
||||
counter.printTopWords(top10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package ca.bcit.comp1510.assignment2.q3;
|
||||
|
||||
/**
|
||||
* <p>This is where you put your description about what this class does. You
|
||||
* don't have to write an essay but you should describe exactly what it does.
|
||||
* Describing it will help you to understand the programming problem better.</p>
|
||||
*
|
||||
* @author Your Name goes here
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Primes {
|
||||
/**
|
||||
* <p>This is the main method (entry point) that gets called by the JVM.</p>
|
||||
*
|
||||
* @param args command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// your code will go here!!!
|
||||
System.out.println("Question three was called and ran sucessfully.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package ca.bcit.comp1510.assignment2.q4;
|
||||
|
||||
/**
|
||||
* <p>This is where you put your description about what this class does. You
|
||||
* don't have to write an essay but you should describe exactly what it does.
|
||||
* Describing it will help you to understand the programming problem better.</p>
|
||||
*
|
||||
* @author Your Name goes here
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Exponential {
|
||||
|
||||
/**
|
||||
* Describe what the main method does here.
|
||||
*
|
||||
* @param args command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
//replace next line with your code:
|
||||
System.out.println("Question four was called and ran sucessfully.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user