P1-P5: rewrite to match current syllabus
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
96
P1.java
96
P1.java
@@ -1,8 +1,92 @@
|
|||||||
class P1 {
|
class Stack {
|
||||||
public static void main(String[] _args) {
|
private int[] array;
|
||||||
int a = 44;
|
private int top;
|
||||||
int b = 56;
|
|
||||||
|
|
||||||
System.out.println(a + " + " + b + " = " + (a + b));
|
public Stack(int capacity) {
|
||||||
}
|
array = new int[capacity];
|
||||||
|
top = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push(int item) {
|
||||||
|
if (top == array.length - 1) {
|
||||||
|
System.out.println("Stack Overflow");
|
||||||
|
} else {
|
||||||
|
array[++top] = item;
|
||||||
|
System.out.println("Pushed " + item + " to the stack");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int pop() {
|
||||||
|
if (top == -1) {
|
||||||
|
System.out.println("Stack Underflow");
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
int item = array[top--];
|
||||||
|
System.out.println("Popped " + item + " from the stack");
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return top == -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Queue {
|
||||||
|
private int[] array;
|
||||||
|
private int front;
|
||||||
|
private int rear;
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
public Queue(int capacity) {
|
||||||
|
array = new int[capacity];
|
||||||
|
front = 0;
|
||||||
|
rear = -1;
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void enqueue(int item) {
|
||||||
|
if (size == array.length) {
|
||||||
|
System.out.println("Queue is full");
|
||||||
|
} else {
|
||||||
|
rear = (rear + 1) % array.length;
|
||||||
|
array[rear] = item;
|
||||||
|
size++;
|
||||||
|
System.out.println("Enqueued " + item + " to the queue");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int dequeue() {
|
||||||
|
if (size == 0) {
|
||||||
|
System.out.println("Queue is empty");
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
int item = array[front];
|
||||||
|
front = (front + 1) % array.length;
|
||||||
|
size--;
|
||||||
|
System.out.println("Dequeued " + item + " from the queue");
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return size == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class P1 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Stack stack = new Stack(5);
|
||||||
|
stack.push(1);
|
||||||
|
stack.push(2);
|
||||||
|
stack.pop();
|
||||||
|
stack.pop();
|
||||||
|
stack.pop();
|
||||||
|
|
||||||
|
Queue queue = new Queue(5);
|
||||||
|
queue.enqueue(1);
|
||||||
|
queue.enqueue(-4);
|
||||||
|
queue.dequeue();
|
||||||
|
queue.dequeue();
|
||||||
|
}
|
||||||
}
|
}
|
20
P2.java
20
P2.java
@@ -1,11 +1,15 @@
|
|||||||
class P2 {
|
import java.util.StringTokenizer;
|
||||||
public static void main(String[] _args) {
|
|
||||||
int a = 5, b, f;
|
|
||||||
b = f = a;
|
|
||||||
|
|
||||||
while (b-- > 1)
|
public class P2 {
|
||||||
f *= b;
|
public static void main(String[] args) {
|
||||||
|
String input = "Hello, I am Batu Khan and I am about to blow.";
|
||||||
|
|
||||||
System.out.println(a + "! = " + f);
|
StringTokenizer tokenizer = new StringTokenizer(input, " ,");
|
||||||
}
|
|
||||||
|
int i = 0;
|
||||||
|
while (tokenizer.hasMoreTokens()) {
|
||||||
|
String token = tokenizer.nextToken();
|
||||||
|
System.out.println("Token " + ++i + ": " + token);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
39
P3.java
39
P3.java
@@ -1,14 +1,27 @@
|
|||||||
class P3 {
|
interface Animal {
|
||||||
public static void main(String[] _args) {
|
void makeSound();
|
||||||
int a = 5, b = -5, c = 55, mx;
|
}
|
||||||
|
|
||||||
if (a > b && a > c)
|
class Cat implements Animal {
|
||||||
mx = a;
|
@Override
|
||||||
else if ( b > a && b > c)
|
public void makeSound() {
|
||||||
mx = b;
|
System.out.println("Meow!");
|
||||||
else
|
}
|
||||||
mx = c;
|
}
|
||||||
|
|
||||||
System.out.println("max(" + a + ", " + b + ", " + c + ") = " + mx);
|
class Cow implements Animal {
|
||||||
}
|
@Override
|
||||||
|
public void makeSound() {
|
||||||
|
System.out.println("Moo!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class P3 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Animal cat = new Cat();
|
||||||
|
Animal cow = new Cow();
|
||||||
|
|
||||||
|
cat.makeSound();
|
||||||
|
cow.makeSound();
|
||||||
|
}
|
||||||
}
|
}
|
66
P4.java
66
P4.java
@@ -1,15 +1,57 @@
|
|||||||
class P4 {
|
class ProducerConsumer {
|
||||||
public static void main(String[] _args) {
|
private int limit = 5;
|
||||||
int n = 10;
|
private int tmp = 0;
|
||||||
int a = 0, b = 1;
|
private int n = 0;
|
||||||
System.out.println("First " + n + " elements of the fibonacci series:");
|
private int m = 0;
|
||||||
new P4().fibo(n, a, b);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fibo(int n, int a, int b) {
|
|
||||||
if (n == 0)
|
public void produce() throws InterruptedException {
|
||||||
return;
|
int value = 0;
|
||||||
System.out.println(a);
|
while (n++ < limit * 2) {
|
||||||
fibo(n - 1, b, a + b);
|
synchronized (this) {
|
||||||
|
while (tmp == limit) {
|
||||||
|
wait();
|
||||||
|
}
|
||||||
|
System.out.println("Producer produced: " + tmp++);
|
||||||
|
notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void consume() throws InterruptedException {
|
||||||
|
while (m++ < limit * 2) {
|
||||||
|
synchronized (this) {
|
||||||
|
while (tmp == 0) {
|
||||||
|
wait();
|
||||||
|
}
|
||||||
|
System.out.println("Consumer consumed: " + --tmp);
|
||||||
|
notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class P4 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ProducerConsumer pc = new ProducerConsumer();
|
||||||
|
|
||||||
|
Thread producerThread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
pc.produce();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Thread consumerThread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
pc.consume();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
producerThread.start();
|
||||||
|
consumerThread.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
43
P5.java
43
P5.java
@@ -1,20 +1,27 @@
|
|||||||
import java.util.function.Consumer;
|
class MyException extends Exception {
|
||||||
|
public MyException(String message) {
|
||||||
class P5 {
|
super(message);
|
||||||
public static void main(String[] _args) {
|
}
|
||||||
int sub[] = { 64, 91, 45 };
|
}
|
||||||
P5 obj = new P5();
|
|
||||||
int sum = 0;
|
public class P5 {
|
||||||
|
public static void main(String[] args) {
|
||||||
for (int i = 0; i < sub.length; i++) {
|
try {
|
||||||
obj.grade(sub[i], i + 1);
|
int result = divide(10, 0);
|
||||||
sum += sub[i];
|
System.out.println("Result: " + result);
|
||||||
}
|
} catch (MyException e) {
|
||||||
|
System.out.println("My Exception: " + e.getMessage());
|
||||||
System.out.println("Overall Percentage: " + (float) sum / 3 + "%");
|
} catch (ArithmeticException e) {
|
||||||
}
|
System.out.println("Arithmetic Exception: " + e.getMessage());
|
||||||
|
} finally {
|
||||||
void grade(int n, int s) {
|
System.out.println("Finally block executed.");
|
||||||
System.out.println( "Subject " + s + " grade: " + (char)( n >= 90 ? 'A' : n < 50 ? 'F' : 'A' + (9 - n / 10)));
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int divide(int num1, int num2) throws MyException {
|
||||||
|
if (num2 == 0) {
|
||||||
|
throw new MyException("Division by zero not allowed.");
|
||||||
|
}
|
||||||
|
return num1 / num2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
155
file.org
155
file.org
@@ -1,155 +0,0 @@
|
|||||||
#+LATEX_CLASS_OPTIONS: [a4paper,12pt]
|
|
||||||
#+LATEX_HEADER: \usepackage[margin=0.5in]{geometry}
|
|
||||||
#+LATEX_HEADER: \usepackage{fontspec}
|
|
||||||
#+LATEX_HEADER: \setmainfont{LiberationSerif}
|
|
||||||
#+LATEX_HEADER: \date{}
|
|
||||||
#+OPTIONS: toc:nil
|
|
||||||
|
|
||||||
|------------------------------------------------------------------------------------------------------------|
|
|
||||||
| Programs are followed by their respective inputs and outputs i.e, both stdin and stdout are shown together |
|
|
||||||
|------------------------------------------------------------------------------------------------------------|
|
|
||||||
|
|
||||||
* Sum of two numbers
|
|
||||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
|
||||||
#+begin_src java :tangle P1.java :results output :exports both :wrap src text
|
|
||||||
class P1 {
|
|
||||||
public static void main(String[] _args) {
|
|
||||||
int a = 44;
|
|
||||||
int b = 56;
|
|
||||||
|
|
||||||
System.out.println(a + " + " + b + " = " + (a + b));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
Output:
|
|
||||||
#+RESULTS:
|
|
||||||
#+begin_src text
|
|
||||||
44 + 56 = 100
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
#+latex: \clearpage
|
|
||||||
|
|
||||||
* Factorial of a number
|
|
||||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
|
||||||
#+begin_src java :tangle P2.java :results output :exports both :wrap src text
|
|
||||||
class P2 {
|
|
||||||
public static void main(String[] _args) {
|
|
||||||
int a = 5, b, f;
|
|
||||||
b = f = a;
|
|
||||||
|
|
||||||
while (b-- > 1)
|
|
||||||
f *= b;
|
|
||||||
|
|
||||||
System.out.println(a + "! = " + f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
Output:
|
|
||||||
#+RESULTS:
|
|
||||||
#+begin_src text
|
|
||||||
5! = 120
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
#+latex: \clearpage
|
|
||||||
|
|
||||||
* Greatest of 3 numbers
|
|
||||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
|
||||||
#+begin_src java :tangle P3.java :results output :exports both :wrap src text
|
|
||||||
class P3 {
|
|
||||||
public static void main(String[] _args) {
|
|
||||||
int a = 5, b = -5, c = 55, mx;
|
|
||||||
|
|
||||||
if (a > b && a > c)
|
|
||||||
mx = a;
|
|
||||||
else if ( b > a && b > c)
|
|
||||||
mx = b;
|
|
||||||
else
|
|
||||||
mx = c;
|
|
||||||
|
|
||||||
System.out.println("max(" + a + ", " + b + ", " + c + ") = " + mx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
Output:
|
|
||||||
#+RESULTS:
|
|
||||||
#+begin_src text
|
|
||||||
max(5, -5, 55) = 55
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
#+latex: \clearpage
|
|
||||||
|
|
||||||
* Fibbonaci series
|
|
||||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
|
||||||
#+begin_src java :tangle P4.java :results output :exports both :wrap src text
|
|
||||||
class P4 {
|
|
||||||
public static void main(String[] _args) {
|
|
||||||
int n = 10;
|
|
||||||
int a = 0, b = 1;
|
|
||||||
System.out.println("First " + n + " elements of the fibonacci series:");
|
|
||||||
new P4().fibo(n, a, b);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fibo(int n, int a, int b) {
|
|
||||||
if (n == 0)
|
|
||||||
return;
|
|
||||||
System.out.println(a);
|
|
||||||
fibo(n - 1, b, a + b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
max(5, -5, 55) = 55
|
|
||||||
#+RESULTS:
|
|
||||||
#+begin_src text
|
|
||||||
First 10 elements of the fibonacci series:
|
|
||||||
0
|
|
||||||
1
|
|
||||||
1
|
|
||||||
2
|
|
||||||
3
|
|
||||||
5
|
|
||||||
8
|
|
||||||
13
|
|
||||||
21
|
|
||||||
34
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
#+latex: \clearpage
|
|
||||||
|
|
||||||
* Percentage marks and grades for 3 subjects
|
|
||||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
|
||||||
#+begin_src java :tangle P5.java :results output :exports both :wrap src text
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
class P5 {
|
|
||||||
public static void main(String[] _args) {
|
|
||||||
int sub[] = { 64, 91, 45 };
|
|
||||||
P5 obj = new P5();
|
|
||||||
int sum = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < sub.length; i++) {
|
|
||||||
obj.grade(sub[i], i + 1);
|
|
||||||
sum += sub[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Overall Percentage: " + (float) sum / 3 + "%");
|
|
||||||
}
|
|
||||||
|
|
||||||
void grade(int n, int s) {
|
|
||||||
System.out.println( "Subject " + s + " grade: " + (char)( n >= 90 ? 'A' : n < 50 ? 'F' : 'A' + (9 - n / 10)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
#+RESULTS:
|
|
||||||
#+begin_src text
|
|
||||||
Subject 1 grade: D
|
|
||||||
Subject 2 grade: A
|
|
||||||
Subject 3 grade: F
|
|
||||||
Overall Percentage: 66.666664%
|
|
||||||
#+end_src
|
|
||||||
|
|
||||||
#+latex: \clearpage
|
|
Reference in New Issue
Block a user