Commit ef0639a8 authored by ik1g19's avatar ik1g19
Browse files

added files

parents
import javafx.scene.control.*;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
public class DumbGUI1 extends Application {
public void start(Stage stage) {
//creating the pane
FlowPane pane = new FlowPane(Orientation.HORIZONTAL);
pane.setAlignment(Pos.CENTER);
//adding a textfield
pane.getChildren().add(new TextField());
//inserting a line between the textfield and buttons
Region newline = new Region();
newline.setPrefSize(Double.MAX_VALUE, 0.0);
pane.getChildren().add(newline);
//adding buttons
pane.getChildren().addAll(new Button("Submit"), new Button("Cancel"));
//setting the distance between nodes
pane.setHgap(10);
pane.setVgap(5);
//creating a scene from the pane
Scene scene = new Scene(pane);
//creating a stage from the scene
stage.setScene(scene);
//setting the default width and height of the stage
stage.setWidth(200);
stage.setHeight(150);
//displaying the stage
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
\ No newline at end of file
/*
* q1.c
*
* Created on: 22 May 2020
* Author: isaac
*/
#include <stdio.h>
#include <math.h>
/*
* declaration of functions
*/
int CACHE(double *prod, double *sum, double d1, double d2);
/*
* returns 1 if two given doubles have the same sign
* otherwise returns -1
*/
int CACHE(double *prod, double *sum, double d1, double d2) {
double d1Round = floor(d1);
double d2Round = floor(d2);
*prod = d1Round * d2Round;
*sum = d1Round + d2Round;
if (d1Round > 0.0 && d2Round > 0.0)
return 1;
else if (d1Round < 0.0 && d2Round < 0.0)
return 1;
else if (d1Round == 0.0 && d2Round == 0.0)
return 1;
else
return -1;
}
/*
* q2.c
*
* Created on: 23 May 2020
* Author: isaac
*/
#include <stdio.h>
/*
* typedef new types
*/
typedef int* NEWTYPE;
/*
* declaration of functions
*/
NEWTYPE ARRAY(int n);
int STORE(NEWTYPE as, int N, int row, int col, int val);
int FETCH(NEWTYPE as, int N, int row, int col);
/*
* creates new array of size n*n
* mallocs memory
*/
NEWTYPE ARRAY(int n) {
NEWTYPE ptr;
ptr = malloc(n * n * sizeof(int));;
return ptr;
}
/*
* stores value at specified row/column
* returns -1 if invalid position
* returns 1 if successfully stored
*/
int STORE(NEWTYPE as, int N, int row, int col, int val) {
if ((row % 2) != (col % 2)) return -1; // returns -1 if row and column have different parity
if (row > N || col > N || row < 0 || col < 0) return -1; // returns -1 if row or column is outside of array
NEWTYPE start = as;
as += row;
as += (col * N);
*as = val;
as = start;
return 1;
}
/*
* fetches value from specified location
* returns -1 if invalid position
* returns stored value if successful
*/
int FETCH(NEWTYPE as, int N, int row, int col) {
if ((row % 2) != (col % 2)) return -1; // returns -1 if row and column have different parity
if (row > N || col > N || row < 0 || col < 0) return -1; // returns -1 if row or column is outside of array
NEWTYPE start = as;
as += row;
as += (col * N);
int val = *as;
as = start;
return val;
}
import javafx.scene.control.*;
import javafx.geometry.Insets;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
public class DumbGUI2 extends Application {
public void start(Stage stage) {
//creating the overall horizontal box
HBox calculator = new HBox();
calculator.setSpacing(10);
calculator.setPadding(new Insets(30, 20, 30, 20));
//creating the input text field
Label variable = new Label("x=");
TextField input = new TextField("1");
calculator.getChildren().addAll(variable, input);
//creating the vertical box for the two buttons
VBox changeValue = new VBox();
Button plusOne = new Button("+1");
Button minusOne = new Button("-1");
changeValue.getChildren().addAll(plusOne, minusOne);
changeValue.setAlignment(Pos.CENTER);
calculator.getChildren().add(changeValue);
//creating the function label
Label function = new Label("Function:");
calculator.getChildren().add(function);
//creating the drop down box for the functions
ChoiceBox functions = new ChoiceBox();
functions.getItems().addAll("X^X", "sin(x)", "cos(x)", "Fibonacci(x)");
functions.setValue("X^X");
calculator.getChildren().add(functions);
//creating the output labels and text box
Label equals = new Label("=");
TextField output = new TextField();
calculator.getChildren().addAll(equals, output);
//creating the slider
Slider slider = new Slider(1, 9, 1);
slider.setShowTickMarks(true);
slider.setShowTickLabels(true);
slider.setMajorTickUnit(2);
slider.setMinorTickCount(1);
calculator.getChildren().add(slider);
//adding action event handlers to the buttons
plusOne.addEventHandler(ActionEvent.ANY, new ButtonHandler(input, slider, 1.0));
minusOne.addEventHandler(ActionEvent.ANY, new ButtonHandler(input, slider, -1.0));
//adding a listener to the input textfield (uses an anonymous inner class)
input.textProperty().addListener(new ChangeListener<String>() {
//when the input text field is changed the output textfield is updated according
//to the selected function
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
int function = functions.getSelectionModel().getSelectedIndex();
Double inputValue = Double.parseDouble(input.getText());
if (function == 0) output.setText(Double.toString(Math.pow(inputValue, inputValue)));
else if (function == 1) output.setText(Double.toString(Math.sin(inputValue)));
else if (function == 2) output.setText(Double.toString(Math.cos(inputValue)));
else if (function == 3) {
output.setText(Double.toString(fibonacci(inputValue)));
};
}
//a recursive function to calculate the fibonacci sequence
private Double fibonacci(Double n) {
if (n.equals(0.0)) return 0.0;
else if (n.equals(1.0)) return 1.0;
else return fibonacci(n - 1.0) + fibonacci(n - 2.0);
}
});
//adding a listener to the slider
slider.valueProperty().addListener(new ChangeListener<Number>() {
//when the slider is changed the incremental buttons are changed accordingly
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
//locks the slider to integer values
slider.setValue(newValue.intValue());
Double value = slider.getValue();
plusOne.setText("+" + Double.toString(value));
minusOne.setText("-" + Double.toString(value));
}
});
//aligning contents of the calculator
calculator.setAlignment(Pos.CENTER);
calculator.setSpacing(20);
//allowing the text boxes to grow and shrink according to the size of the window
HBox.setHgrow(input, Priority.ALWAYS);
HBox.setHgrow(output, Priority.ALWAYS);
//creating a scene from the pane
Scene scene = new Scene(calculator);
//creating a stage from the scene
stage.setScene(scene);
//setting the default width and height of the stage
stage.setWidth(700);
stage.setHeight(150);
//setting the minimum width and height
stage.setMinWidth(1000);
stage.setMinHeight(150);
//displaying the stage
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class ButtonHandler implements EventHandler<ActionEvent> {
private Slider slider;
private TextField input;
private Double action;
private Boolean increment;
public ButtonHandler(TextField input, Slider slider, Double action) {
this.slider = slider;
this.input = input;
this.action = action;
//determines if the button is to increment or decrement
increment = (action > 0) ? true : false;
}
public void handle(ActionEvent event) {
//updating the action value
action = slider.getValue();
Double value = Double.parseDouble(input.getText());
value += increment.equals(true) ? action : -action;
input.setText(Double.toString(value));
}
}
\ No newline at end of file
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import uk.ac.soton.ecs.comp1206.labtestlibrary.io.FileSystemAccess;
/**
* Test for lab 5 on files and IO.
*
* @author jan
*
*/
class FilesIOQ1Test {
static final String CHAR_FILE_PATH = "randomNumbersCharByteFile.txt";
static final String BYTE_FILE_PATH = "randomNumbersByteFile.out";
static final long SEED = 1;
static ArrayList<Integer> randomNumbersByte;
static ArrayList<Integer> randomNumbersChar;
/**
* Creates the random numbers and the java files for the file writing and the java file concatenation tasks, respectively.
* @throws Exception
*/
@BeforeAll
public static void setUpBeforeClass() throws Exception {
initialiseRandomNumbers();
}
/**
* Removes the test folder and test files.
* @throws IOException
*/
@AfterAll
public static void cleanUpAfterClass() throws IOException {
removeTestFiles();
}
/**
* Test that the file written using a char stream was created.
*/
@Test
public void checkCharFileExists() {
File file = new File(CHAR_FILE_PATH);
boolean doseFileExist = file.isFile();
assertTrue(doseFileExist, "Char file does not exist.");
}
/**
* Test that the file written using a char stream contains data.
*/
@Test
public void checkCharFileContainsData() {
File file = new File(CHAR_FILE_PATH);
boolean isFileSizeGreaterZero = (file.length() > 0);
assertTrue(isFileSizeGreaterZero, "Char file has no content.");
}
/**
* Test that the file written using a char stream contains the right data.
*/
@Test
public void checkCharFileRightData() throws IOException {
File file = new File(CHAR_FILE_PATH);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
ArrayList<Integer> readNumbers = new ArrayList<>();
String currLine = "";
while ((currLine = br.readLine()) != null) {
readNumbers.add(Integer.parseInt(currLine));
}
fr.close();
assertEquals(randomNumbersChar, readNumbers, "Read integers are different to expected ones.");
}
/**
* Test that the file written using a byte stream exists.
*/
@Test
public void checkByteFileExists() {
File file = new File(BYTE_FILE_PATH);
boolean doseFileExist = file.isFile();
assertTrue(doseFileExist, "Byte file does not exist.");
}
/**
* Test that the file written using a byte steam contains data.
*/
@Test
public void checkByteFileContainsData() {
File file = new File(BYTE_FILE_PATH);
boolean isFileSizeGreaterZero = (file.length() > 0);
assertTrue(isFileSizeGreaterZero, "Byte file has no content.");
}
/**
* Test that the file written using a byte stream contains the right data.
*/
@Test
public void checkByteFileRightData() throws IOException {
File file = new File(BYTE_FILE_PATH);
FileInputStream fos = new FileInputStream(file);
DataInputStream dos = new DataInputStream(fos);
ArrayList<Integer> readNumbers = new ArrayList<>();
while (dos.available() > 0) {
int num = dos.readInt();
readNumbers.add(num);
}
dos.close();
fos.close();
assertEquals(randomNumbersByte, readNumbers, "Read integers are different to expected ones.");
}
/**
* Uses the seed to calculate all the random number to compare them to the file
* content of the write random number to file exercise.
*
* @throws IOException
*/
private static void initialiseRandomNumbers() throws IOException {
RandomNumberWriter randomNumberWriter = new RandomNumberWriter(SEED);
randomNumberWriter.writeRandomByte(BYTE_FILE_PATH);
randomNumberWriter.writeRandomChars(CHAR_FILE_PATH);
Random random = new Random(SEED);
randomNumbersByte = new ArrayList<Integer>();
randomNumbersChar = new ArrayList<Integer>();
for (int i = 0; i < 20000; i++) {
Integer n = Integer.valueOf(random.nextInt(100000));
if (i < 10000) {
randomNumbersByte.add(n);
} else {
randomNumbersChar.add(n);
}
}
}
/**
* Removes the created test files.
*
* @throws IOException
*/
private static void removeTestFiles() {
FileSystemAccess.deleteFile(new File(CHAR_FILE_PATH));
FileSystemAccess.deleteFile(new File(BYTE_FILE_PATH));
}
}
import uk.ac.soton.ecs.comp1206.labtestlibrary.interfaces.io.RandomIO;
import java.util.Arrays;
import java.util.Random;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.FileWriter;
import java.io.Writer;
import java.nio.ByteBuffer;
//class writes 10000 integers using char and byte streams
public class RandomNumberWriter implements RandomIO {
//random number generator member variable
private Random generator;
//constructor initialises generator with given seed
RandomNumberWriter(long seed) {
generator = new Random(seed);
}
//writes 10000 integers using a byte based stream
public void writeRandomByte(String filename) {
try {
//getting the file ready
File file = new File(filename);
//creating the byte based stream
OutputStream out = new FileOutputStream(file);
//writing 10000 random integers to the file
for (int i = 0; i < 10000; i++) {
//generating a new int
int num = generator.nextInt(100000);
//converting the int to a 4 byte byte array
byte b[] = ByteBuffer.allocate(4).putInt(num).array();
//writing the byte array to the file
out.write(b);
}
//closing the byte stream
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
//writes 10000 integers using a char based stream
public void writeRandomChars(String filename) {
try {
//getting the file ready
File file = new File(filename);
//creating the character based stream
Writer out = new FileWriter(file);
//writing 10000 random integers to the file
for (int i = 0; i < 10000; i++) {
//converts the integer to a string
String num = Integer.toString(generator.nextInt(100000));
//writes the string to the file
out.write(num + System.getProperty("line.separator"));
}
//closing the character stream
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
import uk.ac.soton.ecs.comp1206.labtestlibrary.interfaces.recursion.PalindromeChecker;
//determines if a string is a k-palindrome
public class KPalindrome implements PalindromeChecker {
//returns true if a string is a k-palindrome
public boolean isKPalindrome (String str, int k) {