instruction stringlengths 25 5.05k | output stringlengths 4 8.53k | __index_level_0__ int64 34 78.2k |
|---|---|---|
What is the command to create an array in Java? | The command to create an array in Java is "int[ ] arr = new int[size]". | 77,365 |
Construct a for loop in Java using the following parameters: Initial value of i set to one, condition as i is less than or equal to 5, and iteration of i as subtraction of two. | for (int i = 1; i <= 5; i = i-2){
// Code to be executed
} | 77,409 |
Write a Java method to swap of two integer values. | public static void swap(int x, int y){
int temp = x;
x = y;
y = temp;
} | 77,465 |
Find the maximum pair sum of four integers in an array.
[2, 10, -4, 3, 14, 8] | public class MaximumPairSum {
static int findMaximumPairSum(int arr[]) {
int max1 = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
int max3 = Integer.MIN_VALUE;
int max4 = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max1) { ... | 77,477 |
Provide a Java program for searching for a certain element in an array.
[1, 2, 3, 4, 5], element=4 | public class SearchInArray {
static int search(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
public static void main(String args[]) {
int arr[] = { 1, 2, 3, 4, 5 };
int x = 4; ... | 77,478 |
Create a class in Java which computes the sum of squares of even numbers between k and n. | public class SumSquares {
public static int sumSquares(int k, int n) {
int sum = 0;
for(int i = k; i <= n; i++) {
if (i % 2 == 0) {
sum += (i * i);
}
}
return sum;
}
} | 77,487 |
Write a Java program that passes an array to a method and prints its length. | public class TestArrayLength {
public static void printLength(int[] array) {
System.out.println("The length of the array is: " + array.length);
}
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
printLength(numbers);
}
} | 77,532 |
Construct a program in Java for solving the Tower of Hanoi puzzle. | import java.util.Stack;
public class TowerOfHanoi
{
public static void towersOfHanoi(int disks)
{
Stack<Integer> source = new Stack<Integer>();
Stack<Integer> destination = new Stack<Integer>();
Stack<Integer> auxillary = new Stack<Integer>();
int i, total_moves;
... | 77,540 |
Create a program using Java to print the sum of the following five numbers.
int a = 2;
int b = 5;
int c = 8;
int d = 10;
int e = 13; | public class Sum{
public static void main(String args[])
{
int a = 2;
int b = 5;
int c = 8;
int d = 10;
int e = 13;
int sum = a + b + c + d + e;
System.out.println("Sum = " + sum);
}
} | 77,543 |
Modify the following Java code to debug and execute properly.
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello world);
}
} | public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world);
}
} | 77,554 |
Generate a class in Java for retrieving data from a database. | import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseRetrieve {
private Connection connection;
private String query;
public DatabaseRetrieve(Connection connection, String query) {
this.connection = connection;
... | 77,555 |
Write a Java method using the given parameters.
methodName: sum
parameters: int number1, int number2 | public static int sum(int number1, int number2) {
return number1 + number2;
} | 77,617 |
Modify the given program to print "Hello, foo" where "foo" is user input.
System.out.println("Hello, World!"); | import java.util.Scanner;
public class Test {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter in a name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
}
} | 77,628 |
Create a class in Java that performs the following task: print out all characters of a given string along with their position in the string. | public class PrintPosition {
public static void printPosition(String str) {
for (int i = 0; i < str.length(); i++) {
System.out.println("Character '" + str.charAt(i) + "' is at position " + i);
}
}
} | 77,638 |
Categorize each of the following strings as palindrome or not in Java.
"madam", "DAD", "superman", "abcdef" | Palindrome: madam, DAD
Not Palindrome: superman, abcdef | 77,701 |
Select the correct answer from the following options to complete the Java code:
int a = 12, b = 6;
int result = ________ | a * b; | 77,704 |
Write a Java code to encrypt a password using `sha1` hash.
String password = "password123"; | String hash = MessageDigest.getInstance("SHA-1").digest(password.getBytes(StandardCharsets.UTF_8)); | 77,720 |
Compile a Java class named "Example". | javac Example.java | 77,741 |
Create an object-oriented programming class in Java named "Car" with three private instance variables "make", "speed" and "color". | public class Car {
private String make;
private int speed;
private String color;
public Car() {
this.make = "default";
this.speed = 100;
this.color = "black";
}
public Car(String make, int speed, String color) {
this.make = make;
this.speed ... | 77,748 |
Create a program in Java that takes an array with two integers as input and prints the multiplication of those two input integers.
arr = [3, 4] | public class Main {
public static void main(String[] args) {
int[] arr = {3,4};
int num1 = arr[0];
int num2 = arr[1];
int result = num1 * num2;
System.out.println(result);
}
} | 77,769 |
Create a class Employee in Java which contains the name, age and salary of the employee. | public class Employee {
private String name;
private int age;
private double salary;
public Employee() { }
public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
... | 77,789 |
Create a series of nested for loops in Java to print the numbers from 1 to 10 in descending order on a single line. | for (int i = 10; i >= 1; i--)
System.out.print(i + " "); | 77,856 |
Write an algorithm in Java to reverse an array.
[1, 2, 3, 4, 5] | int[] reverse = new int[arr.length];
int j = arr.length;
// Initializing the index of reverse array
int i;
for (i = 0; i < arr.length; i++)
{
reverse[j - 1] = arr[i];
j--;
} | 77,859 |
Compose a for loop in Java to print the numbers from 4 to 1 one line. | for (int i = 4; i > 0; i--) {
System.out.print(i + " ");
} | 77,864 |
Write a Java program that prompts the user to enter two strings and outputs whether the two strings are anagrams. | import java.util.HashMap;
public class Anagram {
// Utility method to check whether two strings are anagram or not
static boolean areAnagram(String A, String B) {
// If length of both strings is not same, then they can't be anagram
if (A.length() != B.length())
return false;... | 77,874 |
Create a set in Java from a given array of numbers.
Array: [2, 3, 5, 4, 2, 3] | int[] arr = {2, 3, 5, 4, 2, 3};
Set<Integer> set = new HashSet<Integer>();
for (int i : arr) {
set.add(i);
} | 77,905 |
Construct an if-else block in Java to determine if the number 15 is an even number or an odd number.
Integer: 15 | int num = 15;
if(num % 2 == 0)
{
System.out.println("Even");
}
else
{
System.out.println("Odd");
} | 77,936 |
Write a code in Java that will get all the elements from an array that are greater than the given number. | public static List<Integer> getElementsGreaterThan(int[] array, int number) {
List<Integer> result = new ArrayList<>();
for (int element : array) {
if (element > number) {
result.add(element);
}
}
return result;
} | 77,944 |
Create a program that takes two inputs, an array of strings and an array of integers. Output an array with the strings at the same indices as the integers.
String[] strings = {"cat", "dog", "bird", "fish"};
int[] indices = {1, 0, 3, 2}; | String[] output = new String[4];
for(int i = 0; i < strings.length; i++) {
output[indices[i]] = strings[i];
}
System.out.println(Arrays.toString(output)); | 77,955 |
In Java, write a program that prints a certain phrase five times. | public class PrintPhrase {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println("Hello world!");
}
}
} | 77,969 |
With a Java program, convert a string to all lowercase letters.
myString = "HeLlO WoRlD" | String lowerString = myString.toLowerCase();
System.out.println(lowerString); // Outputs "hello world" | 77,980 |
Automatically reformat the following code to be more readable:
for (int i=0;i<n;i++){int j=0;System.out.println("The value of j is: "+j); | for (int i = 0; i < n; i++) {
int j = 0;
System.out.println("The value of j is: " + j);
} | 77,997 |
Develop a Java program that prints all the leap years up to a given year.
Year = 2020 | public class LeapYear {
public static void main(String[] args) {
int year = 2020;
System.out.println("Leap years up to "+year+" are :");
for (int i = 1; i <= year; i++) {
if (i % 4 == 0) {
if (i % 100 == 0) {
if (i % 40... | 78,048 |
Create a method in Java that takes in a string and prints its contents in reverse.
String s= "Hello World!" | public class ReverseString {
public void printReverseString(String s) {
for(int i= s.length()-1; i>=0; i--) {
char c = s.charAt(i);
System.out.print(c);
}
System.out.println();
}
} | 78,083 |
Develop a Java program to check if an input number is an Armstrong number. | public class ArmstrongNumber {
public static void main(String[] args) {
int number = 407;
// sample input
// calculate the length of the number
int lengthOfNumber = String.valueOf(number).length();
int tempNumber = number;
int sum = 0;
while (tempNumber != 0) {
int digit = tempNumbe... | 78,142 |
Generate a for loop in Java for printing integers from 0 to 10. | for (int i = 0; i <= 10; i++) {
System.out.println(i);
} | 78,144 |
Implement a matrix multiplication program in Java. | public class MatrixMultiplication
{
static int N = 5;
static int[][] multiply(int[][] A,
int[][] B)
{
int[][] C = new int[N][N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k... | 78,197 |
Name the coding language used for developing native apps for Android. | Java | 78,210 |
In Java, construct a data structure for storing employee information, including name, age, phone number, and address. | public class Employee {
String name;
int age;
String phoneNumber;
String address;
public Employee(String name, int age, String phoneNumber, String address) {
this.name = name;
this.age = age;
this.phoneNumber = phoneNumber;
this.address = address;
}
// Getters and Setters
....
} | 78,216 |
Write an algorithm in Java to reverse the words in a string.
The quick brown fox | String[] words = input.split("\\s");
String reversedString = "";
for (int i = words.length - 1; i >= 0; i--)
{
reversedString += words[i] + " ";
}
System.out.println(reversedString); | 78,217 |
Create a Java web application that displays a welcome message. | import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/welcome")
public class WelcomeSer... | 78,223 |
Write a method in Java to return a reversed version of a given string. | public static String reverseString(String s) {
StringBuilder sb = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
sb.append(s.charAt(i));
}
return sb.toString();
} | 78,241 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.