id
stringlengths 36
36
| text
stringlengths 1
1.25M
|
---|---|
b6a93b24-28a6-4e9a-83fe-7d9dff136a1d
|
void appendEvent(String str) {
event.append(str);
event.setCaretPosition(event.getText().length() - 1);
}
|
7cf59325-59ba-4d83-a9ac-91864b101b00
|
public void actionPerformed(ActionEvent e) {
if ((e.getActionCommand().equals("stopStart"))) {
// if running we have to stop
if (server != null) {
server.stop();
server = null;
tPortNumber.setEditable(true);
stopStart.setText("Start");
return;
}
// OK start the server
int port;
try {
port = Integer.parseInt(tPortNumber.getText().trim());
} catch (Exception er) {
appendEvent("Invalid port number");
return;
}
// ceate a new Server
server = new Server(port, this);
// and start it as a thread
new ServerRunning().start();
stopStart.setText("Stop");
tPortNumber.setEditable(false);
}
else {
ServerGUIList b = new ServerGUIList();
b.setVisible(true);
}
}
|
591db538-539d-4165-851f-e73c3aae9ec3
|
public static void main(String[] arg) {
// ์๋ฒ๋ฅผ ๊ธฐ๋ณธ 1500 ํฌํธ๋ก ์์.
new ServerGUI(1500);
}
|
92f463fc-c116-4ee3-b811-2c882c8eaffc
|
public void windowClosing(WindowEvent e) {
// ๋ง์ฝ ์๋ฒ๊ฐ ์ข
๋ฃ ๋๋ฉด ์๋ฒ๋ฅผ ๋ซ๋๋ค.
if (server != null) {
try {
server.stop(); // ์ฐ๊ฒฐ์ ์ข
๋ฃํ๋๋ก ์๋ฒ์ ํต๋ณดํ๋ค.
} catch (Exception eClose) {
}
server = null;
}
dispose();
System.exit(0);
}
|
455e0184-94a9-4760-9250-641f1439b060
|
public void windowClosed(WindowEvent e) {
}
|
003042de-d0a5-4a80-af02-a8cf6c13c88d
|
public void windowOpened(WindowEvent e) {
}
|
e7bda4ef-3adc-4c86-a71d-299bbc8db1bf
|
public void windowIconified(WindowEvent e) {
}
|
0679e906-4ed2-43fe-917f-7b116217fcfb
|
public void windowDeiconified(WindowEvent e) {
}
|
2ccb779c-b2d9-4196-bb2f-0a8d53464d09
|
public void windowActivated(WindowEvent e) {
}
|
5388b6d0-3185-44b5-b3e0-9bfb1e6b99e4
|
public void windowDeactivated(WindowEvent e) {
}
|
b5e69667-ba1f-487c-a538-004d85a3d66e
|
public void run() {
server.start(); // ์๋ฒ๋ฅผ ์์์ํจ๋ค.
// ์ถฉ๋์ด ์ผ์ด๋๋ ๊ฒฝ์ฐ.
stopStart.setText("Start");
tPortNumber.setEditable(true);
appendEvent("Server crashed\n");
server = null;
}
|
ceb074c7-ee7b-4aa6-baa4-6751c00b7d44
|
public static void main(String[] args) {
MainFrame mf = new MainFrame();
}
|
f328c38d-297f-4624-b36e-9065aa956659
|
public ConnectDB() {
Connection conn;
Statement stmt;
ResultSet rs;
String sql;
try {
Class.forName("com.mysql.jdbc.Driver");// ๋๋ผ์ด๋ฒ ๋ก๋ฉ: DriverManager์ ๋ฑ๋ก
} catch (ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
}
System.out.println("1. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ");
try {
String jdbcUrl = "jdbc:mysql://211.199.232.249:3306/eboard";// ์ฌ์ฉํ๋
// ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ช
์
// ํฌํจํ url
String userId = "root";// ์ฌ์ฉ์๊ณ์
String userPass = "dksgytkd";// ์ฌ์ฉ์ ํจ์ค์๋
conn = DriverManager.getConnection(jdbcUrl, userId, userPass);// Connection
// ๊ฐ์ฒด๋ฅผ
// ์ป์ด๋
stmt = conn.createStatement();// Statement ๊ฐ์ฒด๋ฅผ ์ป์ด๋
// stmt.executeUpdate("use test"); // DB์ ์ฐ๊ฒฐ
System.out.println("2. ์ ๋๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค");// ์ฑ๊ณต์ ํ๋ฉด์ ํ์๋จ
// ๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ฒ๋ฆฌ๋ฌธ
sql = "insert into test(name) value('dkssud')";
for (int i = 0; i < 10; i++)
stmt.executeUpdate(sql);
System.out.println("3. ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์
๋ ฅ ์๋ฃ.");
// ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฒ๋ฆฌ๋ฌธ ์ข
๋ฃ
stmt.close();
conn.close();
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
}
|
e71069a6-e7ba-4eee-a375-7eabb474748e
|
public static void main (String args[]) {
int choice;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of stacks: ");
numStack = scan.nextInt();
System.out.println("Enter the stack size: ");
stackSize = scan.nextInt();
size = numStack * stackSize;
int array[] = new int[size];
int num, stackNum;
Boolean key = true;
do {
System.out.println("....MENU....");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display stack");
System.out.println("Choose your option: ");
choice = scan.nextInt();
switch(choice) {
case 1:
System.out.println("Enter the stack number: ");
stackNum = scan.nextInt();
System.out.println("Enter the number to be added: ");
num = scan.nextInt();
array = push(num, stackNum, array);
break;
case 2:
System.out.println("Enter the stack number: ");
stackNum = scan.nextInt();
array = pop(stackNum, array);
break;
case 3:
display(array);
break;
default:
System.out.println("Incorrect option chosen.");
break;
}
System.out.println("Continue? true/false");
key = scan.nextBoolean();
} while(key);
}
|
828306fe-5354-43ca-b4e6-8d1c49b3553e
|
public static int[] push(int num, int stackNum, int array[]) {
if(stackNum < 0) {
System.out.println("Stack number cannot be negative. Aborting push()....");
return array;
}
int calc = (stackNum - 1) * stackSize;
if(calc > array.length) {
System.out.println("Incorrect stack number. Aborting push()....");
return array;
}
int tempSize = 0;
while(tempSize < stackSize && calc < size) {
if(array[calc] == 0) {
array[calc] = num;
break;
}
tempSize++;
calc++;
}
return array;
}
|
df180b11-4388-4ad0-863e-0a98967e1096
|
public static int[] pop(int stackNum, int array[]) {
if(stackNum < 0) {
System.out.println("Stack number cannot be negative. Aborting pop()....");
return array;
}
int calc = (stackNum - 1) * stackSize;
if(calc > array.length) {
System.out.println("Incorrect stack number. Aborting pop()....");
return array;
}
int tempSize = 0, flash = 0;
if (!(array[calc] == 0)) {
while (tempSize < stackSize && calc < size) {
if (array[calc + 1] == 0) {
System.out.println(array[calc] + "popped");
array[calc] = 0;
flash = 1;
break;
}
tempSize++;
calc++;
}
}
if(flash == 0) {
System.out.println("Empty stack. Nothing was popped.");
}
return array;
}
|
5c38f167-cd87-4eff-bd78-2cc457afaf54
|
public static void display(int array[]) {
for(int i = 0; i < size; i++) {
System.out.print(array[i]);
}
}
|
121bf58b-5a2f-4207-852f-a689b6ecf70e
|
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter number1: ");
String num1 = scan.next();
System.out.println("Enter number2: ");
String num2 = scan.next();
boolean isInt1, isInt2;
isInt1 = isInteger(num1);
isInt2= isInteger(num2);
long n1, n2;
if(!isInt1 || !isInt2) {
System.out.println("The strings entered are not numeric.");
System.exit(0);
}
n1 = Integer.parseInt(num1);
n2 = Integer.parseInt(num2);
multiply(n1, n2);
}
|
46bd51b7-e140-465b-a64e-340d4f3f1f4f
|
public static boolean isInteger(String num) {
try {
Integer.parseInt(num);
} catch (NumberFormatException e) {
return false;
}
return true;
}
|
482d3cd9-a470-42d5-a242-3bf510d09c66
|
public static void multiply(long num1, long num2) {
System.out.println(num1 + " * " + num2 + " = " + num1*num2);
}
|
f2653dcb-5061-4e59-8202-b035596925aa
|
public static void main(String args[] ) throws Exception {
try (Scanner scanner = new Scanner(System.in)) {
int num = Integer.parseInt(scanner.nextLine());
int arr[] = new int[num];
for (int i = 0; i < num; i++) {
arr[i] = scanner.nextInt();
}
printReverseLevel(arr);
} catch (Exception e) {
throw e;
}
}
|
2ca45727-dae3-4a33-b5e6-ce5ce6e1f01a
|
public static void printReverseLevel(int arr[])
{
if(arr.length == 0)
return;
Map<Integer, ArrayList<Integer>> levels = new HashMap<>();
int i = 0;
int maxLevel = 0;
int currentLevel = 1;
while(i < arr.length && arr[i] != -1)
{
i++;
}
// should have a root element
assert(i < arr.length);
// Root level
ArrayList<Integer> list = new ArrayList<>();
list.add(i);
levels.put(currentLevel, list);
Queue<Integer> queue = new LinkedBlockingQueue<>();
queue.add(i);
while(!queue.isEmpty())
{
int element = queue.remove();
currentLevel += 1;
for(int j = 0; j < arr.length; j++)
{
if(arr[j] == element)
{
queue.add(j);
ArrayList<Integer> currentList = levels.get(currentLevel);
if(currentList == null)
{
currentList = new ArrayList<Integer>();
}
currentList.add(j);
levels.put(currentLevel, currentList);
if(currentLevel > maxLevel)
{
maxLevel = currentLevel;
}
}
}
}
while (maxLevel >= 1)
{
ArrayList<Integer> nodes = levels.get(maxLevel);
maxLevel--;
for(int position : nodes)
{
System.out.print(position + " ");
}
System.out.println();
}
}
|
1aec1f00-3476-4bbc-9791-9b7713e41a33
|
public static void main(String args[]) {
String word1, word2;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the first word: ");
word1 = scan.next();
System.out.println("Enter the second word: ");
word2 = scan.next();
findNoncommon(word1, word2);
}
|
0724cc0e-b18c-4ab3-9177-06432229ab80
|
public static void findNoncommon(String word1, String word2) {
HashSet<Character> h1 = new HashSet<Character>();
HashSet<Character> h2 = new HashSet<Character>();
for(int i = 0; i < word1.length(); i++) {
h1.add(word1.charAt(i));
}
for(int i = 0; i < word2.length(); i++) {
h2.add(word2.charAt(i));
}
char[] w1, w2;
w1 = word1.toCharArray();
w2 = word2.toCharArray();
StringBuilder sb = new StringBuilder();
for(char i : w1) {
if(!h2.contains(i))
sb.append(i);
}
for(char i : w2) {
if(!h1.contains(i))
sb.append(i);
}
System.out.println(sb.toString());
}
|
31ceabfd-f98d-4534-b74c-509ed7ba2f26
|
public static void main(String[] args) {
test();
}
|
42af3fba-6759-41fd-b989-dc60f4438df7
|
public static long stringToLong(String num) {
if (null == num || num.length() == 0) {
System.out.println("Empty entry.");
return -1;
}
Boolean res = checkIfNum(num);
if (!res) {
return -1;
}
int len = num.length();
int i = 0;
Boolean neg = false;
long result = 0;
while (i < len) {
if (i == 0 && num.charAt(i) == '-') {
neg = true;
} else {
result = result * 10 + (num.charAt(i) - '0');
}
i++;
}
if (neg) {
result *= (-1);
}
return result;
}
|
fb12bbf0-7cec-4c9c-aeea-edcf1d5f991d
|
public static Boolean checkIfNum(String num) {
return num.matches("[-+]?\\d*\\d+");
}
|
486fab32-4997-4a41-886c-c11ccfe1139d
|
public static void test() {
String num;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number: ");
num = scan.next();
System.out.println("NUMBER IS: " + stringToLong(num));
}
|
a22598d2-07d6-412a-91a8-90647c03ed51
|
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
String input;
try {
while (!(input = scan.nextLine()).equals("")) {
int num = Integer.parseInt(input);
if (num < 0) {
System.out.println(FAIL);
return;
}
list.add(num);
}
} catch (NoSuchElementException e) {
// e.printStackTrace();
} catch (NumberFormatException e) {
System.out.println(FAIL);
return;
}
if (list.size() <= 0) {
System.out.println(FAIL);
return;
}
Integer arr[] = new Integer[list.size()];
arrayHopper(list.toArray(arr));
}
|
3f325978-6a52-478c-8299-f2747d4982f2
|
public static void arrayHopper(Integer arr[]) {
if (null == arr || arr.length == 0)
return;
int dp[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
if (i == 0) {
dp[0] = 0;
} else {
dp[i] = Integer.MAX_VALUE;
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < i; j++) {
if (arr[j] + j >= i) {
dp[i] = Math.min(dp[i], dp[j] + 1);
break;
}
}
}
if (dp[arr.length - 1] == Integer.MAX_VALUE) {
System.out.println("failure");
return;
}
int prev = dp[arr.length - 1];
int old = arr.length - 1;
int i = arr.length - 1;
List<Integer> hops = new ArrayList<Integer>();
while (i >= 0) {
hops.add(old);
int newOld = 0;
while (i >= 0 && (dp[i] == prev)) {
i--;
}
while (i >= 0 && dp[i] == prev - 1) {
if (arr[i] + i >= old) {
newOld = i;
}
i--;
}
old = newOld;
prev = prev - 1;
}
hops.add(old);
printHops(hops);
}
|
8c52597b-8547-4916-bf24-5783d074ab49
|
public static void printHops(List<Integer> hops) {
for (int i = hops.size() - 1; i >= 0; i--) {
System.out.print(hops.get(i) + ", ");
}
System.out.print("out");
}
|
87251c07-9067-4759-b595-7c0afee2d7bc
|
public Node(int num) {
this.item = num;
}
|
0bd5f7ad-8b4a-4363-b552-482f753f3926
|
public TrinaryTree(Node node) {
this.node = node;
}
|
e1d32d30-7acf-43a8-bffd-b5fcd05e958c
|
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of nodes in the tree: ");
int num = scan.nextInt();
TrinaryTree root = new TrinaryTree(null);
if (num <= 0) {
System.out.println("No elements to enter in the tree");
return;
}
for (int i = 0; i < num; i++) {
root.node = insertTree(scan.nextInt(), root.node);
}
System.out.println("Initial tree");
printTree(root.node);
System.out.println("\nEnter node to delete: ");
deleteNode(scan.nextInt(), root.node);
System.out.println("Tree after deletion");
printTree(root.node);
}
|
d1663f4f-657b-4d1f-a0ea-d8f5409f15bb
|
public static Node insertTree(int num, Node root) {
if (root == null) {
Node node = new Node(num);
root = node;
return root;
}
if (root.item == num) {
Node node = new Node(num);
root.center = node;
return root;
}
if (num < root.item) {
root.left = insertTree(num, root.left);
} else {
root.right = insertTree(num, root.right);
}
return root;
}
|
d65dcb6d-2e0d-4c6a-b55c-f3b8d8049656
|
public static Node deleteNode(int value, Node root) {
if (null == root) {
System.out.println(value + " does not exist in the tree");
return null;
}
if (value < root.item) {
root.left = deleteNode(value, root.left);
} else if (value > root.item) {
root.right = deleteNode(value, root.right);
} else {
if (root.left != null && root.right != null) {
Node min = getMin(root.right);
root.item = min.item;
if (root.center != null) {
root.center.item = min.item;
}
root.right = deleteNode(min.item, root.right);
} else if (root.left != null) {
root = root.left;
return root;
} else if (root.right != null) {
root = root.right;
return root;
} else {
return null;
}
}
return root;
}
|
340245d6-b396-4ce3-bea8-d531f083ea37
|
public static Node getMin(Node root) {
if (null == root) {
return null;
}
if (root.left == null) {
return root;
}
return getMin(root.left);
}
|
2169c4fb-8267-477b-bb30-5332d4dcbc48
|
public static void printTree(Node root) {
if (root == null) {
return;
}
printTree(root.left);
System.out.print(root.item + " ");
if (root.center != null)
System.out.print(root.center.item + " ");
printTree(root.right);
}
|
40dbee10-2939-45d6-b9ea-accaa0022d28
|
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number: ");
int num = scan.nextInt();
if(num < 0) {
System.out.println("Number is negative. Cannot decode....");
return;
}
int len = (int) Math.log10(num) + 1;
int res = 0;
res += decodeEachAlpha(num, len);
res += decodeFromBeginning(num, len);
res += decodeFromEnd(num, len);
System.out.println(num + " can be encoded " + res + " number of ways.");
}
|
664d0d08-8298-43c5-bb25-98837c908b81
|
public static int decodeEachAlpha(int num, int len) {
StringBuilder sb = new StringBuilder();
int res;
for(int i = 0; i < len; i++) {
res = num % 10;
res -= 1;
res += 97;
sb.append((char) res);
num = num / 10;
}
sb.reverse();
System.out.println(sb.toString());
if(sb.length() > 0)
return 1;
else
return 0;
}
|
323e99aa-87eb-4bd3-b0cd-7fbcaf0368b3
|
public static int decodeFromBeginning(int num, int len) {
StringBuilder sb = new StringBuilder();
int res, pow, toPow;
while (num != 0) {
if(len - 2 > 0)
toPow = len - 2;
else if(len - 1 > 0)
toPow = len - 1;
else
toPow = 0;
pow = (int) Math.pow(10, toPow);
res = (int) num / pow;
if (res < 26) {
num -= (res * pow);
res -= 1;
res += 97;
sb.append((char) res);
if(pow != 1)
len -= 2;
else
len -= 1;
}
}
System.out.println(sb.toString());
if(sb.length() > 0)
return 1;
else
return 0;
}
|
8783b920-f494-4957-bc7c-c9dae7da93ec
|
public static int decodeFromEnd(int num, int len) {
StringBuilder sb = new StringBuilder();
int res;
for(int i = 0; i < len; i++) {
if(num % 100 < 26) {
res = num % 100;
res -= 1;
res += 97;
sb.append((char) res);
num = num / 100;
i++;
} else {
res = num % 10;
res -= 1;
res += 97;
sb.append((char) res);
num = num / 10;
}
}
sb.reverse();
System.out.println(sb.toString());
if(sb.length() > 0)
return 1;
else
return 0;
}
|
6210696d-4c78-4cd8-b7bb-b41bf02e7532
|
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of elements in the list: ");
int num = scan.nextInt();
System.out
.println("-----------Enter the price of an ounce of gold on each of the "
+ num + " days-----------");
int[] arr = new int[num];
for (int i = 0; i < num; i++) {
System.out.println("Enter the price of gold on day " + i + ": ");
arr[i] = scan.nextInt();
}
findBestProfit(arr);
for (int i = 0; i < num; i++)
System.out.print(arr[i] + " ");
}
|
cb50463f-5222-4894-8c01-56d2863451b2
|
public static void findBestProfit(int[] arr) {
if (null == arr || arr.length <= 0) {
System.out.println("Empty cost record.");
return;
}
if (arr.length == 1) {
System.out.println("Best Profit is: " + 0);
return;
}
int min = 0;
int[] best = new int[2];
min = arr[0];
best[0] = arr[0];
best[1] = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] - min > best[1] - best[0]) {
best[0] = min;
best[1] = arr[i];
}
}
int profit = best[1] - best[0];
System.out.println("Best profit is when the gold is bought for "
+ best[0] + " and sold for " + best[1] + " making a profit of "
+ profit);
}
|
750e34d9-ec42-457d-bc5f-5cae40c33ed1
|
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = scan.next();
Boolean res = checkIfUnique(str);
if (res) {
System.out.println(str + " has unique characters!");
} else {
System.out.println(str + " does not have unique characters!");
}
}
|
cf775845-34b8-4e5c-bf74-684a9eff9c95
|
public static Boolean checkIfUnique(String str) {
int len = str.length();
if (len == 0 || null == str) {
System.out.println("Empty string!");
System.exit(0);
}
if (len > 256) {
return false;
}
boolean[] charfound = new boolean[256];
for (int i = 0; i < len; i++) {
int num = str.charAt(i);
if (charfound[num]) {
return false;
} else {
charfound[num] = true;
}
}
return true;
}
|
771cb340-994a-4e9d-a361-8242298966de
|
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
createList(list);
System.out.println(list);
findKthElement(list);
}
|
049dca35-0c26-4073-8e70-8429c4ce8646
|
public static void createList(LinkedList<Integer> list) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of elements in the list: ");
int num = scan.nextInt();
for (int i = 0; i < num; i++) {
System.out.println("Enter the number: ");
list.add(scan.nextInt());
}
}
|
813e2ffe-dd0a-491a-814a-4e6ce27ea845
|
public static void findKthElement(LinkedList<Integer> list) {
if(list.isEmpty() || null == list) {
System.out.println("Empty list");
return;
}
Scanner scan = new Scanner(System.in);
System.out.println("Enter the position of the element: ");
int pos = scan.nextInt();
if(pos > list.size()) {
System.out.println("Position number exceeds list size");
return;
}
int size = list.size();
int pos1 = 0;
while(pos1 < pos) {
pos1++;
}
int pos2 = 0;
while(pos1 < size) {
pos1++;
pos2++;
}
System.out.println(pos + " position to the last element is: " + list.get(pos2));
}
|
9198f8f5-3333-4ea3-b61b-a3e0e9bcfefb
|
public static void main(String[] args) {
// TODO Auto-generated method stub
}
|
57a502b8-6eab-4b13-aa90-2e12d592ea34
|
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
createList(list);
System.out.println(list);
removeDuplicates(list);
System.out.println("After removal of duplicates: " + list);
}
|
8e60a969-b619-4ab3-8fcf-169a0b41fec9
|
public static void createList(LinkedList<Integer> list) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of elements in the list: ");
int num = scan.nextInt();
for (int i = 0; i < num; i++) {
System.out.println("Enter the number: ");
list.add(scan.nextInt());
}
}
|
ef986d9f-f82e-4325-b1b8-93eae922bbaf
|
public static void removeDuplicates(LinkedList<Integer> list)
{
int size = list.size();
int i = 0;
while(i < size)
{
int j = i + 1;
while(j < size)
{
if(list.get(i) == list.get(j))
{
list.remove(j);
size = list.size();
}
j++;
}
i++;
}
}
|
2b5f58f6-a6ba-400c-926a-8373bd82ed50
|
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the string1: ");
String str1 = scan.next();
System.out.println("Enter the string2: ");
String str2 = scan.next();
boolean res = checkPermutation(str1, str2);
if (res) {
System.out.println(str2 + " is a permutation of " + str1);
} else {
System.out.println(str2 + " is not a permutation of " + str1);
}
}
|
8858bb82-4554-458e-9533-95d099071458
|
public static boolean checkPermutation(String str1, String str2) {
if (null == str1 || null == str2 || str1.isEmpty() || str2.isEmpty()) {
return false;
}
if (str1.length() != str2.length()) {
return false;
}
Map<Character, Integer> map = new HashMap<Character, Integer>();
int len = str1.length() - 1;
while (len >= 0) {
if (map.containsKey(str1.charAt(len))) {
Integer val = map.get(str1.charAt(len)) + 1;
map.put(str1.charAt(len), val);
} else {
map.put(str1.charAt(len), 0);
}
len--;
}
len = str2.length() - 1;
while (len >= 0) {
if (!map.containsKey(str2.charAt(len))) {
return false;
}
len--;
}
return true;
}
|
d84de9d7-21ce-4ca9-9edb-5489e2463bae
|
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = scan.nextLine();
replaceSpace(str);
}
|
d293ce67-dd79-463b-b192-5c4e8dbb106e
|
public static void replaceSpace(String str) {
if (null == str) {
System.out.println("Empty String!");
System.exit(0);
}
String result = new String();
for (int i = 0; i < str.length(); i++) {
result += ((str.charAt(i) == ' ') ? "%20" : str.charAt(i));
}
System.out.println(result);
}
|
d5ccbe13-b776-47ef-a4bd-b5f846f5f730
|
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = scan.next();
str = compressedString(str);
System.out.println("Compressed string: " + str);
}
|
87c4bcd2-f543-4089-98aa-a04d99704787
|
public static String compressedString(String str) {
if (null == str) {
System.out.println("Empty String!");
System.exit(0);
}
if (str.length() == 1) {
return str;
}
int len = str.length();
int count = 0;
String res = new String();
res += str.charAt(0);
count++;
for (int i = 1; i < len; i++) {
if (str.charAt(i) != str.charAt(i - 1)) {
res = res + count + str.charAt(i);
count = 1;
} else {
count++;
}
}
res += count;
if (res.length() < str.length()) {
return res;
} else {
return str;
}
}
|
43496768-fff7-4d57-be62-89c265e15ceb
|
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = scan.next();
Boolean res = checkIfUnique(str);
if (res) {
System.out.println(str + " has unique characters!");
} else {
System.out.println(str + " does not have unique characters!");
}
}
|
3234df04-c96f-485b-8d0f-ad8235daa759
|
public static Boolean checkIfUnique(String str) {
int len = str.length();
if (len == 0 || null == str) {
System.out.println("Empty string!");
System.exit(0);
}
if (len > 256) {
return false;
}
boolean[] charfound = new boolean[256];
for (int i = 0; i < len; i++) {
int num = str.charAt(i);
if (charfound[num]) {
return false;
} else {
charfound[num] = true;
}
}
return true;
}
|
84e9cff5-d5c9-4606-9031-e285bc5c3df1
|
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = scan.next();
reverse(str);
}
|
77515a47-d111-40f9-889a-5fc3bcd31afd
|
public static void reverse(String str) {
if (str.length() == 0 || null == str) {
System.out.println("Empty string!");
System.exit(0);
}
StringBuilder sb = new StringBuilder(str);
int end = str.length() - 1;
int start = 0;
while (start <= end) {
char temp = str.charAt(start);
sb.setCharAt(start, str.charAt(end));
sb.setCharAt(end, temp);
start++;
end--;
}
System.out.println("Reversed string: " + sb.toString());
}
|
b714d582-adcf-4239-82e4-bd27f1d75809
|
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the string1: ");
String str1 = scan.nextLine();
System.out.println("Enter the string2: ");
String str2 = scan.nextLine();
isSubstring(str1, str2);
}
|
03e1f8d0-6770-4468-8c95-9ba5802f89ec
|
public static void isSubstring(String str1, String str2) {
if (str1.isEmpty() || str2.isEmpty() || null == str1 || null == str2) {
System.out
.println("Cannot perform check, the string(s) entered might be empty");
System.exit(0);
}
if (str1.length() != str2.length()) {
System.out.println(str2 + " is not a rotation of " + str1);
return;
}
String joined = str1 + str1;
if (joined.contains(str2)) {
System.out.println(str2 + " is a rotation of " + str1);
} else {
System.out.println(str2 + " is not a rotation of " + str1);
}
}
|
dbac1b5e-9647-459e-8dd6-155887bf405a
|
public MapList(){
maplist = new ArrayList<Map>();
}
|
9786138e-d0a0-425a-9e53-cfc3f951727f
|
public void addMap(String mapName, String fileName){
Map map = new Map(mapName);
map.readMap(fileName);
maplist.add(map);
}
|
34fc00c9-097e-461e-bda6-57461037938a
|
public static void leftClick(){
try{
Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(clickDelay);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (AWTException e) {
e.printStackTrace();
}
}
|
9f4b1f84-2a5d-49a0-94d8-ee9be2c3b59a
|
public static void press(int key){
try{
Robot robot = new Robot();
robot.keyPress(key);
robot.delay(pressDelay);
robot.keyRelease(key);
} catch (AWTException e) {
e.printStackTrace();
}
}
|
a48b9dae-1918-4001-8a5a-5a980821624d
|
Pokedex() {
pokemonList = new ArrayList();
}
|
63d1ddfb-874e-41b6-8107-0977423c6722
|
public void addPokemon() {
Scanner in = new Scanner(System.in);
String pokemonName = null;
try {
pokemonName = in.nextLine();
System.out.println(pokemonName);
Thread.sleep(4000);
pokemonList.add(PokemonIdentify.newPokemon(pokemonName));
} catch (Exception e) {
e.printStackTrace();
System.out.println("IO error trying to read the pokemon name!");
System.exit(1);
}
}
|
e3ac57c0-82f0-4bb5-8b2f-c4e44a6814ad
|
public void move(ArrayList<String> moveList){
for(String move : moveList){
switch (move) {
case "u": Controls.up(); break;
case "d": Controls.down(); break;
case "l": Controls.left(); break;
case "r": Controls.right(); break;
case "a": Controls.a(); break;
case "b": Controls.b(); break;
default: System.out.println("Illegal Move!"); break;
}
}
}
|
d723678c-bfff-4d82-a573-ed97ec24cddf
|
public static void main(String[] args) {
Pokedex pokedex;
MapList mapList;
ObjectInputStream objIn;
//Loading poke.dex file
try{
objIn = new ObjectInputStream(new FileInputStream("Poke.dex"));
pokedex = (Pokedex) objIn.readObject();
objIn.close();
System.out.println("Pokedex loaded successfully");
} catch (IOException | ClassNotFoundException e) {
pokedex = new Pokedex();
System.out.println("Pokedex did not load successfully");
}
//Loading list.maps file
try{
objIn = new ObjectInputStream(new FileInputStream("list.maps"));
mapList = (MapList) objIn.readObject();
objIn.close();
System.out.println("Maplist loaded successfully");
} catch (IOException | ClassNotFoundException e) {
mapList = new MapList();
System.out.println("Maplist did not load successfully");
}
}
|
33e44370-1cf6-47dd-999b-02df8140b89c
|
public static void a(){
ClickAndKeyPress.press(KeyEvent.VK_Z);
}
|
0c99a4bf-5356-4209-9c93-9b6fbe0e3235
|
public static void b(){
ClickAndKeyPress.press(KeyEvent.VK_X);
}
|
49e836cb-361d-49fa-aa68-34396e26b5aa
|
public static void up(){
ClickAndKeyPress.press(KeyEvent.VK_UP);
}
|
abdf0994-7e41-4abb-8887-15665ba56fbb
|
public static void down(){
ClickAndKeyPress.press(KeyEvent.VK_DOWN);
}
|
cb862de8-1c6e-45ad-a651-c7a0ea2cbe73
|
public static void left(){
ClickAndKeyPress.press(KeyEvent.VK_LEFT);
}
|
44cebfdc-0bb3-43ec-aa53-4ae935e798f3
|
public static void right(){
ClickAndKeyPress.press(KeyEvent.VK_RIGHT);
}
|
99b0e5c2-4948-496e-9b1b-9d0bcb034681
|
public static Color[] getColors(){
try {
Color[] colors = new Color[5];
Robot robot = new Robot();
colors[0] = robot.getPixelColor(firstPixel[0],firstPixel[1]);
colors[1] = robot.getPixelColor(secondPixel[0],secondPixel[1]);
colors[2] = robot.getPixelColor(thirdPixel[0],thirdPixel[1]);
colors[3] = robot.getPixelColor(fourthPixel[0],fourthPixel[1]);
return colors;
} catch (AWTException e) {
e.printStackTrace();
}
return null;
}
|
32d7dbc9-fa99-4bf7-bcde-2694197cb9af
|
public static Pokemon newPokemon(String name){
return (new Pokemon(name, getColors()));
}
|
60c8eed4-90fa-4e49-8874-8258602176d0
|
public Map(String mapName){
this.name = mapName;
this.map = null;
}
|
845e1bbd-42f7-4982-a7d9-967f6937081a
|
public void readMap(String fileName){
try{
File file = new File(fileName);
in = new Scanner(file);
width = in.nextInt();
height = in.nextInt();
for (int i = 0; height > i; i++){
char[] line = in.nextLine().toCharArray();
for (int j = 0; width > j; j++){
map[j][i] = line[j];
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
|
4e1ca259-6f7e-4d75-8740-8437da9af863
|
public boolean isRock(int x, int y){
return (map[x][y] == rock);
}
|
c158c89c-3887-445e-8c1d-e2bf6a17f92a
|
public boolean isWater(int x, int y){
return (map[x][y] == water);
}
|
ad0b747e-6c26-4bc9-993f-44a1c6ab8b02
|
public boolean isGrass(int x, int y){
return (map[x][y] == grass);
}
|
562674d6-5dc0-4b49-a795-321861945fe7
|
public boolean isRoad(int x, int y){
return (map[x][y] == road);
}
|
35d1f400-95d0-4b34-856a-445fee859946
|
Pokemon(String name, Color[] colorList){
this.name = name;
first = colorList[0];
second = colorList[1];
third = colorList[2];
fourth = colorList[3];
}
|
1d5f289e-af0c-4c06-99a7-c82a308a98d8
|
public String run(ArrayDeque<imAtoken> x, ArrayList<Functions> y, String conti)
{
while(!x.isEmpty())
{
//create global variable list
SymTable symtab = new SymTable();
decList.add(symtab);
program(x,y);
}
conti = "The code can be parsed";
return conti;
}
|
815e7672-a1f0-4968-8867-376d944e2e1c
|
public void program(ArrayDeque<imAtoken> x, ArrayList<Functions> y)
{
declist(x,y);
}
|
c218f834-5901-42c6-a908-d32f51673460
|
public void declist(ArrayDeque<imAtoken> x, ArrayList<Functions> y)
{
declaration(x,y);
decloop(x,y);
}
|
68d53529-5465-499b-b417-b407deb16f89
|
public void declaration(ArrayDeque<imAtoken> x, ArrayList<Functions> y)
{
if(!x.isEmpty())
{
typeSpec(x,y);
imAtoken token = x.peek();
if(token.type.equals("ID"))
{
for(int i = decList.size() - 1; i > 0; i--)
{
for(int v = 0; v < decList.get(i).stacker.size() - 1; v++)
{
if (decList.get(i).lookUp(v).ID.equals(token.name))
{
ptype.add(decList.get(i).lookUp(i).type);
System.out.println("Variable " + token.name + " has been defined before");
}
else if (i == 0)
{
System.out.println("variable " + token.name + " is not defined");
System.exit(0);
}
}
}
//Capture declaration ID
ID = token.name;
//remove token
x.pop();
}
else
{
System.out.println("Error1: expected an ID but got a " + token.type + " on line " + token.line);
System.exit(0);
}
decFollow(x,y);
}
}
|
a101bacd-0327-4d18-9eb3-2dee46abcbd1
|
public void decloop(ArrayDeque<imAtoken> x, ArrayList<Functions> y)
{
if(!x.isEmpty())
{
imAtoken token = x.peek();
if( token.name.equals("int") || token.name.equals("void") || token.name.equals("float") )
{
declaration(x,y);
decloop(x,y);
}
}
}
|
b9b241c5-7ab2-4515-b829-b9ba53854ad7
|
public void typeSpec(ArrayDeque<imAtoken> x, ArrayList<Functions> y)
{
imAtoken token = x.peek();
if( token.name.equals("int") || token.name.equals("void") || token.name.equals("float") )
{
//capture declaration type
type = token.name;
//remove token
x.pop();
}
else
{
System.out.println("Error2: expected an int, void, or float but found " + token.name + " on line " + token.line);
System.exit(0);
}
}
|
caa6185c-b320-48cc-aa79-cfdf17365c78
|
public void decFollow(ArrayDeque<imAtoken> x, ArrayList<Functions> y)
{
//decfollow -> (params) compound | X
boolean duplicate = true;
if(!x.isEmpty())
{
imAtoken token = x.peek();
if(token.name.equals("("))
{
x.pop();
params(x,y);
if(!x.isEmpty())
{
token = x.peek();
if(token.name.equals(")"))
{
if(functionList.size() != 0)
{
//check for duplicate function
for(int t = 0; t <= (functionList.size()-1); t++)
{
if(name.equals(functionList.get(t).name))
{
for(int s = 0; s < (functionList.get(s).pType.size()-1); s++)
{
if(!ptype.get(s).equals(functionList.get(s).pType.get(s)))
{
duplicate = false;
}
}
}
else if (t == functionList.size()-1)
{
duplicate = false;
}
}
}
else
{
duplicate = false;
}
if(!duplicate)
{
//create a Function
Functions func = new Functions(ID, type, ptype, pname, isAr);
//Send function to Function list
functionList.add(func);
System.out.println("function " + type + " " + ID + " found");
//reset functions
name = "";
type = "";
ptype.clear();
pname.clear();
isAr = false;
}
else
{
System.out.println("error function " + type + " " + ID + " already defined");
System.exit(0);
}
x.pop();
compound(x,y);
}
}
}
else if(token.name.equals(";") || token.name.equals("["))
{
X(x,y);
}
else
{
System.out.println("Error3: Expected \"(\" or \";\" or \"[\" but found " + token + " on line " + token.line);
System.exit(0);
}
}
else
{
System.out.println("Error: Out of Tokens");
}
}
|
2936ed3e-7df5-4de0-815e-2b08d0599806
|
public void params(ArrayDeque<imAtoken> x, ArrayList<Functions> y)
{
//params-> int ID paramtype parLoop | float ID paramtype parLoop | void parameter
if(!x.isEmpty())
{
imAtoken token = x.peek();
if(token.name.equals("int") || token.name.equals("float"))
{
//capture parameter type
ptype.add(token.name);
//remove token
x.pop();
if(!x.isEmpty())
{
token = x.peek();
System.out.println("you shouldnt see this -> " + token.name);
if(token.type.equals("ID"))
{
//capture param name
pname.add(token.name);
x.pop();
paramType(x,y);
parLoop(x,y);
}
}
else
{
System.out.println("Error: Out of Tokens");
System.exit(0);
}
}
else
{
if(token.name.equals("void"))
{
//capture parameter type
ptype.add(token.name);
pname.add("");
x.pop();
parameter(x,y);
}
else
{
System.out.println("Error4: Expected int, float, or void but found " + token + " on line " + token.line);
System.exit(0);
}
}
}
}
|
49852efe-69aa-4a0e-9bc0-6fdedc3b94fe
|
public void compound(ArrayDeque<imAtoken> x, ArrayList<Functions> y)
{
//compound-> { localDecs statementList }
if(!x.isEmpty())
{
imAtoken token = x.peek();
if(token.name.equals("{"))
{
if(ifWhile == 0)
{
//create a new symbol table
SymTable symTab = new SymTable();
//add table to List
decList.add(symTab);
}
//remove token
x.pop();
localDecs(x, y);
statementList(x, y);
if (!x.isEmpty()) {
token = x.peek();
if (token.name.equals("}"))
{
if(ifWhile > 0)
{
ifWhile--;
}
else if(ifWhile == 0)
{
//drop current scope
decList.remove(decList.size() - 1);
}
//remove token
x.pop();
}
else
{
System.out.println("Error5: expected \"}\" but found " + token.name + " on line " + token.line);
System.exit(0);
}
}
else
{
System.out.println("Error: Out of Tokens");
System.exit(0);
}
}
else
{
System.out.println("Error6: expected \"{\" but found " + token.name + " on line " + token.line);
System.exit(0);
}
}
else
{
System.out.println("Error: Out of Tokens");
}
}
|
52d6b7ba-502a-49b2-88ef-340cc88b3246
|
public void X(ArrayDeque<imAtoken> x, ArrayList<Functions> y)
{
//X-> ; | [NUM] ;
Array = false;
boolean duplicate = true;
if(!x.isEmpty())
{
imAtoken token = x.peek();
if(token.name.equals(";"))
{
if(decList.get(decList.size()-1).stacker.size() != 0)
{
//check for duplicate variable
for(int t = 0; t <= decList.get(decList.size()-1).stacker.size()-1; t++)
{
if(!ID.equals(decList.get(decList.size()-1).lookUp(t).ID))
{
duplicate = false;
}
}
}
else
{
duplicate = false;
}
if(!duplicate)
{
Declaration dec = new Declaration(ID, type, arraySize, Array);
//add declaration to table
decList.get(decList.size()-1).insert(dec);
System.out.println(dec.type + " " + dec.ID + " found");
}
else
{
System.out.println("Error1:" + ID + " already declared in this scope");
System.exit(0);
}
//remove token
x.pop();
}
else
{
if(token.name.equals("["))
{
//declare it an array
Array = true;
x.pop();
NUM(x,y);
if(!x.isEmpty())
{
token = x.peek();
if (token.name.equals("]"))
{
x.pop();
if (!x.isEmpty()) {
token = x.peek();
if (token.name.equals(";"))
{
if(((decList.get(decList.size()-1).stacker.size()))!= 0)
{
//check for duplicate variable
for(int t = 0; t <= decList.get(decList.size()-1).stacker.size()-1; t++)
{
if(!ID.equals(decList.get(decList.size()-1).lookUp(t).ID))
{
duplicate = false;
}
else if(t == decList.get(decList.size()-1).stacker.size()-1 )
{
duplicate = false;
}
}
}
else
{
duplicate = false;
}
if(!duplicate)
{
Declaration dec = new Declaration(ID, type, arraySize, Array);
//add dec to table
decList.get(decList.size()-1).insert(dec);
System.out.println(dec.type + "[" + arraySize + "] " + dec.ID + " found");
}
else
{
System.out.println("Error2:" + ID + " already declared in this scope");
System.exit(0);
}
//remove token
x.pop();
}
else
{
System.out.println("Error7: expected \";\" but found " + token.name + " on line " + token.line);
System.exit(0);
}
}
else
{
System.out.println("Error: Out of Tokens");
}
}
else
{
System.out.println("Error8: expected \"]\" but found " + token.name + " on line " + token.line);
System.exit(0);
}
}
}
else
{
System.out.println("Error9: expected \"[\" but found " + token.name + " on line " + token.line);
System.exit(0);
}
}
}
else
{
System.out.println("Error: out of Tokens");
}
}
|
21cb775f-b697-4eea-898d-764547633741
|
public void NUM(ArrayDeque<imAtoken> x, ArrayList<Functions> y)
{
if(!x.isEmpty())
{
imAtoken token = x.peek();
if(token.type.equals("float") || token.type.equals("int"))
{
if(Array && token.type.equals("int"))
{
//capture length of array
arraySize = Integer.parseInt(token.name);
}
else if(Array && token.type.equals("float"))
{
System.out.println("Arrays can not be of size \"float\"");
System.exit(0);
}
//remove token
x.pop();
}
else
{
System.out.println("Error10: expected an int or a float but found a " + token.type + " on line " + token.line);
System.exit(0);
}
}
else
{
System.out.println("Error: Out of Tokens");
}
}
|
fc43fc3b-7954-4c72-919d-1ca5b7230f53
|
public void localDecs(ArrayDeque<imAtoken> x, ArrayList<Functions> y)
{
//localDecs-> typeSpec ID X localDecs | empty
if(!x.isEmpty())
{
imAtoken token = x.peek();
if(token.name.equals("int") || token.name.equals("void") || token.name.equals("float"))
{
typeSpec(x,y);
token = x.peek();
if(token.type.equals( "ID"))
{
if(type.equals("void"))
{
System.out.println("Error: variables can not be of type void");
System.exit(0);
}
else
{
//capture the ID
ID = token.name;
}
//remove the token
x.pop();
X(x,y);
localDecs(x,y);
}
else
{
System.out.println("Error: Expected an ID found " + token + " on line " + token.line);
System.exit(0);
}
}
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.