Skip to content

Commit 96b399e

Browse files
committed
Merge
2 parents 53b63b6 + acd0dc7 commit 96b399e

21 files changed

Lines changed: 688 additions & 21 deletions

DigitalMax.java

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

1111
int n=in.nextInt();

Dijkstra.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
this program is used to find minimum distance between two vertices while traversing all available vertices.
33
*/
4-
4+
55
import java.util.*;
66

77
public class Dijkstra {
@@ -51,7 +51,7 @@ void dijkstra(int graph[][], int src)
5151
}
5252

5353

54-
dist[src] = 0;
54+
dist[src] = 0; /* Starting distance is zero */
5555

5656

5757
for (int count = 0; count < V-1; count++)
@@ -85,7 +85,7 @@ public static void main (String[] args)
8585
{
8686
Scanner in=new Scanner(System.in);
8787

88-
System.out.println("Number of vertices:");
88+
System.out.println("Number of vertices:"); /* print the total numbers of vertices */
8989

9090
V=in.nextInt();
9191

@@ -99,7 +99,7 @@ public static void main (String[] args)
9999

100100
graph[i][j]=in.nextInt();
101101
}
102-
Dijkstra ob=new Dijkstra();
102+
Dijkstra ob=new Dijkstra(); /* create new object of Dijkstra */
103103

104104
ob.dijkstra(graph, 0);
105105
}

Dine.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ public static void main(String[] args){
77
int x=10;
88

99
Log.msg(String.valueOf(x));
10-
10+
/* Initializing 5 chopsticks */
1111
Chopstick[] chopistics = new Chopstick[5];
1212

1313
for(int i=0; i< chopistics.length; i++){
1414

1515
chopistics[i] = new Chopstick("C: "+i);
1616
}
1717

18+
// Initialising five philosophers and their corresponding positions
1819
Philosopher[] philosophers = new Philosopher[5];
1920

2021
philosophers[0] = new Philosopher("P: 0 - ", chopistics[0], chopistics[1]);
@@ -38,7 +39,7 @@ public static void main(String[] args){
3839
}
3940
}
4041

41-
42+
//Class for philosophers
4243
class Philosopher extends Thread
4344
{
4445
private Chopstick leftChopistick;
@@ -62,6 +63,9 @@ public Philosopher ( String name, Chopstick left, Chopstick right){
6263

6364
public void eat()
6465
{
66+
// While eating, every philosopher pics up the left chopstick first and the the right chopstick. If the philosopher gets both the chopsticks
67+
// he eats for 1000 ms and then releases the chopsticks
68+
//Else he waits for chopsticks to be free
6569
if(! leftChopistick.used){
6670

6771
if(!rightChopistick.used){
@@ -115,6 +119,7 @@ public static void Delay(int ms){
115119
}
116120
}
117121

122+
//Class for chopstick that provides use and release functionalities to pilosophers
118123
class Chopstick{
119124

120125
public boolean used;
@@ -125,13 +130,15 @@ public Chopstick(String name){
125130

126131
this.name = name;
127132
}
128-
133+
//method to reserve the chopstick for use
129134
public synchronized void take() {
130135

131136
Log.msg ("Used :: " + name );
132137

133138
this.used = true;
134139
}
140+
141+
//method to release the chopstick for use
135142
public synchronized void release() {
136143

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

JavaDateAndTime.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Arrays;
2+
import java.util.Calendar;
3+
import java.util.List;
4+
import java.util.Scanner;
5+
6+
public class JavaDateAndTime {
7+
static List<String> days = Arrays.asList("SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY");
8+
9+
/**
10+
* Gives the Day of the week(SUNDAY,MONDAY...) based on the passed params
11+
* @param day
12+
* @param month
13+
* @param year
14+
* @return
15+
*/
16+
public static String getDay(String day, String month, String year) {
17+
18+
int y = Integer.parseInt(year);
19+
int m = Integer.parseInt(month);
20+
int d = Integer.parseInt(day);
21+
22+
Calendar c = Calendar.getInstance();
23+
c.set(y, m-1, d);
24+
25+
int p = c.get(Calendar.DAY_OF_WEEK);
26+
String s = days.get(p-1);
27+
return s;
28+
}
29+
30+
public static void main(String[] args) {
31+
Scanner in = new Scanner(System.in);
32+
String month = in.next();
33+
String day = in.next();
34+
String year = in.next();
35+
36+
System.out.println(getDay(day, month, year));
37+
38+
}
39+
}

NumberToWords.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*Given a number, you have to write it in words*/
2+
import java.io.*;
3+
import java.util.*;
4+
class NumberToWords
5+
{
6+
public static void main(String[] args)throws IOException
7+
{
8+
DataInputStream in=new DataInputStream(System.in);
9+
int t,l,x,y;String s,ans;
10+
String l19[]={"","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine ","Ten ","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};
11+
String l90[]={"","","Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "};
12+
//System.out.println("Enter input:");
13+
t=Integer.parseInt(in.readLine());
14+
while(t>0)
15+
{ans="";int c=12;
16+
s=in.readLine();l=s.length();int n[]=new int[13];
17+
for(int i=l-1;i>=0;i--,c--)
18+
{
19+
n[c]=s.charAt(i)-48;
20+
}y=0;
21+
for(int i=++c;i<13;i++)
22+
{
23+
switch(i)
24+
{
25+
case 0:if(n[i]==0)break;ans=ans+l19[n[i]]+"Trillion ";break;
26+
case 1:
27+
case 7:
28+
case 4:
29+
case 10:if(n[i]==0)break;ans=ans+l19[n[i]]+"Hundred ";y=n[i];break;
30+
case 2:
31+
case 5:
32+
case 8:
33+
case 11:x=n[i]*10+n[i+1];y=y*100+x;if(y==0)break;if(x<20)ans=ans+l19[x];else ans=ans+l90[n[i]]+l19[n[i+1]];i++;break;
34+
case 3:
35+
case 6:
36+
case 9:
37+
case 12:y=n[i];if(y==0)break;ans=ans+l19[y];break;
38+
}
39+
if(i-1==2&&y!=0){ans=ans+"Billion ";y=0;}
40+
if(i-1==5&&y!=0){ans=ans+"Million ";y=0;}
41+
if(i-1==8&&y!=0){ans=ans+"Thousand ";y=0;}
42+
}
43+
System.out.println(ans.trim());
44+
t--;
45+
}
46+
}
47+
}

SizeOfNumber.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.io.*;
2+
import java.util.*;
3+
class SizeOfNumber {
4+
public static void main(String[] args) {
5+
Scanner sc = new Scanner(System.in);
6+
int t = sc.nextInt();
7+
8+
for (int i = 0; i < t; i++) {
9+
try {
10+
long x = sc.nextLong();
11+
System.out.println(x + " can be fitted in:");
12+
if (x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE) System.out.println("* byte");
13+
if (x >= Short.MIN_VALUE && x <= Short.MAX_VALUE) System.out.println("* short");
14+
if (x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE) System.out.println("* int");
15+
if (x >= Long.MIN_VALUE && x <= Long.MAX_VALUE) System.out.println("* long");
16+
} catch (Exception e) {
17+
System.out.println(sc.next() + " can't be fitted anywhere.");
18+
}
19+
20+
}
21+
sc.close();
22+
}
23+
}

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: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

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

66
public static void main(String args[])
@@ -10,24 +10,21 @@ public static void main(String args[])
1010
Scanner in=new Scanner(System.in);
1111

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

1616
System.out.println("Player 2 :");
17-
17+
// get name2
1818
String name2=in.nextLine();
19-
19+
// 2D array of 3 elements each
2020
char[][] m=new char[3][3];Setm(m);
2121

2222

2323
int r,c,count=0,u=1;
2424

25-
while(count<=9)
26-
{
27-
if(count==9)
28-
29-
break;
30-
25+
// 9 moves in TicTacToe game
26+
while(count<9)
27+
{
3128
RefPos(u);
3229

3330
r=in.nextInt();

University.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/*
22
this program is used to find minimum distance between two vertices while traversing all available vertices.
3-
*/
3+
*/
44
import java.util.*;
55

6+
/**
7+
* This class is to represnt a Student
8+
*
9+
*/
610
class Students
711
{
812
String name="";
@@ -21,7 +25,7 @@ class Students
2125
}
2226

2327
Students(){}
24-
28+
//Student setter method
2529
void setstu(String name,int roll,double cgpa)
2630
{
2731
this.name=name;
@@ -30,11 +34,17 @@ void setstu(String name,int roll,double cgpa)
3034

3135
this.cgpa=cgpa;
3236
}
37+
//Student getter method
3338
void getstu()
3439
{
3540
System.out.println(name+"\t"+roll+"\t"+cgpa);
3641
}
3742
}
43+
44+
/**
45+
* This class is to represent a Semester with containing students and other information
46+
*
47+
*/
3848
class Sem
3949
{
4050
ArrayList<Students> li;
@@ -88,6 +98,10 @@ void showsem()
8898
}
8999
}
90100
}
101+
/**
102+
* This class represnts a Course in a Institute
103+
*
104+
*/
91105
class Course
92106
{
93107
String name;
@@ -137,6 +151,10 @@ void showcourse()
137151
}
138152
}
139153
}
154+
/**
155+
* This class represents a Department in a Institute
156+
*
157+
*/
140158
class Department
141159
{
142160
String name;
@@ -186,6 +204,10 @@ void showdept()
186204
}
187205
}
188206
}
207+
/**
208+
* This class represents an Institute
209+
*
210+
*/
189211
class Institute
190212
{
191213
String name;
@@ -235,6 +257,10 @@ void showinst()
235257
}
236258
}
237259
}
260+
/**
261+
* This class represents a University containing one or more Institute
262+
*
263+
*/
238264
public class University {
239265

240266
public static void main(String[] args)

0 commit comments

Comments
 (0)