Skip to content

Commit 856482c

Browse files
authored
Merge branch 'master' into master
2 parents 56aa2e5 + ae30d6d commit 856482c

6 files changed

Lines changed: 189 additions & 13 deletions

File tree

DigitalMax.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class DigitalMax {
77
public static void main(String[] agrs)
88
{
99
Scanner in=new Scanner(System.in);
10-
10+
//User will enter a number
1111
System.out.println("Enter a number :");
1212

1313
int n=in.nextInt();

Dine.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Program for the classic dining philosopher problem in operating system */
12
package javacodes;
23

34

@@ -8,14 +9,15 @@ public static void main(String[] args){
89
int x=10;
910

1011
Log.msg(String.valueOf(x));
11-
12+
/* Initializing 5 chopsticks */
1213
Chopstick[] chopistics = new Chopstick[5];
1314

1415
for(int i=0; i< chopistics.length; i++){
1516

1617
chopistics[i] = new Chopstick("C: "+i);
1718
}
1819

20+
// Initialising five philosophers and their corresponding positions
1921
Philosopher[] philosophers = new Philosopher[5];
2022

2123
philosophers[0] = new Philosopher("P: 0 - ", chopistics[0], chopistics[1]);
@@ -39,7 +41,7 @@ public static void main(String[] args){
3941
}
4042
}
4143

42-
44+
//Class for philosophers
4345
class Philosopher extends Thread
4446
{
4547
private Chopstick leftChopistick;
@@ -63,6 +65,9 @@ public Philosopher ( String name, Chopstick left, Chopstick right){
6365

6466
public void eat()
6567
{
68+
// While eating, every philosopher pics up the left chopstick first and the the right chopstick. If the philosopher gets both the chopsticks
69+
// he eats for 1000 ms and then releases the chopsticks
70+
//Else he waits for chopsticks to be free
6671
if(! leftChopistick.used){
6772

6873
if(!rightChopistick.used){
@@ -116,6 +121,7 @@ public static void Delay(int ms){
116121
}
117122
}
118123

124+
//Class for chopstick that provides use and release functionalities to pilosophers
119125
class Chopstick{
120126

121127
public boolean used;
@@ -126,13 +132,15 @@ public Chopstick(String name){
126132

127133
this.name = name;
128134
}
129-
135+
//method to reserve the chopstick for use
130136
public synchronized void take() {
131137

132138
Log.msg ("Used :: " + name );
133139

134140
this.used = true;
135141
}
142+
143+
//method to release the chopstick for use
136144
public synchronized void release() {
137145

138146
Log.msg ("Released :: " + name );

Root.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1+
/* This program is to find value of root node in a tree
2+
when value of each node and sum of child nodes of all nodes is given. */
3+
14
package javacodes;
25

3-
import java.util.*;
6+
import java.util.*; //import packages
47

5-
public class Root{
8+
public class Root{
69

710
public static void main(String[] args)
811
{
912
Scanner in=new Scanner(System.in);
1013

1114
System.out.println("Number of vertices in tree");
1215

13-
int v=in.nextInt();
16+
int v=in.nextInt(); //taking number of vertices as input
1417

1518
int sumt=0,sumc=0,tmp;
1619

1720
System.out.println("Values of vertices");
1821

1922
for(int i=1;i<=v;i++)
2023
{
21-
tmp=in.nextInt();
24+
tmp=in.nextInt(); //taking values of each node as input
2225

2326
sumt+=tmp;
2427
}

SumOfPairs.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package javacodes;
2+
3+
/**
4+
* Given an array of ints A and an int m, find out if there exists
5+
* a pair of elements in A that add up to m in a relatively fast way.
6+
*/
7+
public class SumOfPairs {
8+
9+
public static boolean sumExists(int[] list, int m) {
10+
int[] differences = new int[list.length];
11+
12+
for (int i = 0; i < list.length; i++) {
13+
int curr = list[i];
14+
15+
// ignore the trivial case where m is an element of the list.
16+
if (curr == m) {
17+
continue;
18+
}
19+
20+
int diff = m - curr;
21+
22+
for (int j = 0; j <= i; j++) {
23+
int currDiff = differences[j];
24+
25+
if (currDiff == diff || currDiff == curr) {
26+
27+
System.out.println("A pair is " + curr + " and " + diff + "\n");
28+
return true;
29+
}
30+
}
31+
32+
differences[i] = diff;
33+
}
34+
35+
return false;
36+
}
37+
38+
public static void main(String[] args) {
39+
int[] l = {1, 8, 10, 3, 2, 6};
40+
41+
System.out.println(sumExists(l, 8));
42+
}
43+
}

TicTacToe.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package javacodes;
22

33
import java.util.Scanner;
4-
4+
/** class TicTacToe takes in user input and prints the game to the screen*/
55
public class TicTacToe {
66

77
public static void main(String args[])
@@ -11,13 +11,13 @@ public static void main(String args[])
1111
Scanner in=new Scanner(System.in);
1212

1313
System.out.println("Enter name\nPlayer 1 :");
14-
14+
// get name1
1515
String name1=in.nextLine();
1616

1717
System.out.println("Player 2 :");
18-
18+
// get name2
1919
String name2=in.nextLine();
20-
20+
// 2D array of 3 elements each
2121
char[][] m=new char[3][3];Setm(m);
2222

2323

@@ -31,7 +31,7 @@ public static void main(String args[])
3131

3232
break;
3333

34-
//print to show which player's turn
34+
//print to show which player's turn
3535
RefPos(u);
3636

3737
r=in.nextInt();

fractional_knapsack

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/* Java code for fractional knapsack problem - Greedy Approach */
2+
import java.util.ArrayList;
3+
import java.util.Arrays;
4+
import java.util.Iterator;
5+
import java.util.Scanner;
6+
7+
public class fractional_knapsack {
8+
9+
private double finalValue;
10+
private int remainingWeight;
11+
private Item [] itemList;
12+
13+
public fractional_knapsack( int totalWeight, Item [] itemList) {
14+
this.finalValue = 0;
15+
this.remainingWeight = totalWeight;
16+
this.itemList = itemList;
17+
//Sorting the array in descending order based upon the preference order
18+
Arrays.sort(itemList);
19+
}
20+
21+
public double getFinalValue() {
22+
return finalValue;
23+
}
24+
25+
public int getRemainingWeight() {
26+
return remainingWeight;
27+
}
28+
29+
public Item [] getFractions(){
30+
int i = 0;
31+
32+
//Setting fraction of preffered items as 1.0, if their weight is less than the total remaining weight
33+
while(i < itemList.length && remainingWeight > itemList[i].getWeight()){
34+
remainingWeight -= itemList[i].getWeight();
35+
finalValue += itemList[i].getValue();
36+
itemList[i].setFraction(1.0);
37+
i++;
38+
}
39+
40+
if( i < itemList.length) {
41+
//Calculating the fraction of the item whoes weight is greater than the current remaining weight
42+
finalValue = finalValue + (remainingWeight) * itemList[i].getValue() / itemList[i].getWeight();
43+
itemList[i].setFraction(remainingWeight / itemList[i].getWeight());
44+
remainingWeight = 0 ;
45+
}
46+
47+
return itemList;
48+
}
49+
50+
public static void main(String args[]){
51+
int totalWeight = 90;
52+
Item [] items = {new Item(10,60) , new Item(20,100) , new Item(30, 120)};
53+
54+
fractional_knapsack fractionalKnapsack = new fractional_knapsack(totalWeight , items);
55+
56+
items = fractionalKnapsack.getFractions();
57+
System.out.println("TOTAL VALUE = "+fractionalKnapsack.getFinalValue()+" REMAINING WEIGHT = "+fractionalKnapsack.getRemainingWeight());
58+
for(int i = 0 ; i < items.length ; i++)System.out.print("ITEM "+(i+1) +" "+items[i]);
59+
}
60+
61+
}
62+
63+
64+
65+
class Item implements Comparable<Item>{
66+
private int weight;
67+
private int value;
68+
69+
70+
private double preference;
71+
private double fraction;
72+
73+
public Item(int weight, int value) {
74+
75+
this.weight = weight;
76+
this.value = value;
77+
this.fraction = 0.0;
78+
//Attribute preference helps to decide the order of preference of the items for selection
79+
this.preference = (double) value / (double) weight;
80+
}
81+
82+
public int getWeight() {
83+
return weight;
84+
}
85+
86+
public void setWeight(int weight) {
87+
this.weight = weight;
88+
}
89+
90+
public int getValue() {
91+
return value;
92+
}
93+
94+
public void setValue(int value) {
95+
this.value = value;
96+
}
97+
98+
public double getFraction() {
99+
return fraction;
100+
}
101+
102+
public void setFraction(double fraction) {
103+
this.fraction = fraction;
104+
}
105+
106+
107+
//Enabling sort in descending order
108+
@Override
109+
public int compareTo(Item item) {
110+
double difference = this.preference - item.preference;
111+
if(difference > 0)
112+
return -1;
113+
else if(difference < 0)
114+
return 1;
115+
else
116+
return 0;
117+
}
118+
@Override
119+
public String toString(){
120+
return "VALUE = "+this.value+" WEIGHT = "+this.weight+" FRACTION = "+this.fraction+"\n";
121+
}
122+
}

0 commit comments

Comments
 (0)