CSC1109_Tutorials/Exam/FIB.java

210 lines
4.8 KiB
Java

package Exam;
import java.util.*;
public class FIB {
public static int fib(int a){
if(a == 0 || a == 1) return a;
return fib(a-1) + fib(a-2);
}
public static int fac(int a){
if (a <= 1) return a;
else return a * fac(a - 1);
}
public static int[] mergesort(int[] arr){
if (arr.length <= 1) {
return arr;
}
int mid = arr.length >> 1;
int[] left = new int[mid];
int[] right = new int[arr.length - mid];
for (int i = 0; i < mid; i++){
left[i] = arr[i];
}
for (int i = mid; i < arr.length; i++){
right[i - mid] = arr[i];
}
left = mergesort(left);
right = mergesort(right);
return merge(left, right);
}
public static int[] merge(int[] a, int[] b){
int aindex = 0, bindex = 0;
int [] result = new int[a.length + b.length];
int aSize = a.length, bSize = b.length;
while (aindex < a.length && bindex < bSize){
if (a[aindex] < b[bindex]){
result[aindex + bindex] = a[aindex];
aindex++;
}
else{
result[aindex + bindex] = b[bindex];
bindex++;
}
}
while (aindex < aSize){
result[aindex + bindex] = a[aindex];
aindex++;
}
while (bindex < bSize){
result[aindex + bindex] = b[bindex];
bindex++;
}
return result;
}
public static ArrayList<Integer> mergesort(ArrayList<Integer> arr){
if (arr.size() <= 1) {
return arr;
}
int mid = arr.size() >> 1;
ArrayList<Integer> left = new ArrayList<>();
ArrayList<Integer> right = new ArrayList<>();
for (int i = 0; i < mid; i++){
left.add(arr.get(i));
}
for (int i = mid; i < arr.size(); i++){
right.add(arr.get(i));
}
left = mergesort(left);
right = mergesort(right);
return merge(left, right);
}
public static ArrayList<Integer> merge(ArrayList<Integer> a, ArrayList<Integer> b){
ArrayList<Integer> c = new ArrayList<>();
int aindex = 0, bindex = 0;
int aSize = a.size(), bSize = b.size();
while (aindex < aSize && bindex < bSize){
if (a.get(aindex) < b.get(bindex)){
c.add(a.get(aindex));
aindex++;
}
else{
c.add(b.get(bindex));
bindex++;
}
}
while (aindex < aSize){
c.add(a.get(aindex));
aindex++;
}
while (bindex < bSize){
c.add(b.get(bindex));
bindex++;
}
return c;
}
public static int BSearc(int[] arr, int num){
int low = 0, high = arr.length;
while (low <= high){
int mid = (low + high) >> 1;
if (arr[mid] == num) return mid;
else if (arr[mid] < num) low = mid + 1;
else high = mid - 1;
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9,10};
int num = 4;
System.out.println(BSearc(arr, num));
}
}
class person{
private String name;
private int age;
public person(String name, int age){
System.out.println("Person constructor");
this.name = name;
this.age = age;
}
public person(String name){
this(name, 0);
}
public person(int age){
this("John", age);
}
public String getName(){
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge(){
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}
class Student extends person{
private int GPA;
private double height;
public Student(){
this("John", 0, 0);
}
public Student(String name, int age, int GPA){
super(name, age);
this.GPA = GPA;
}
public Student(double height){
this("John", 0, 0);
this.height = height;
System.out.println("Student height: " + height);
}
private Student(String name, int GPA){
this(name, 0, GPA);
}
public Student(int GPA, int age, String name){
this(name, age, GPA);
}
public Student(int age, String name){
this(name, age, 0);
}
public void setAge(int age){
System.out.printf("Student age: %d \n", age);
super.setAge(age);
}
public static void main(String... args){
var s = new Student(3);
System.out.println(s.getAge());
}
}
class myexception extends Exception{
public myexception(String message){
super(message);
}
}