instruction stringlengths 25 5.05k | output stringlengths 4 8.53k | __index_level_0__ int64 34 78.2k |
|---|---|---|
Compile the following Java code and explain the output.
class Test {
static int x = 8;
static int y = 3;
static int z;
static void addition() {
z = x + y;
System.out.println("The sum: " + z);
} | // Output when compiled:
The sum: 11
// Explanation: The above code assigns 'x' to 8, 'y' to 3 and 'z' to the sum of 'x' and 'y' (11). The program then prints out the value of 'z' which is 11. | 63,207 |
Translate the given code into Java.
let x = 10; | int x = 10; | 63,261 |
Code an if statement in Java to check if an integer is odd | if (num % 2 == 1) {
// num is odd
} | 63,266 |
Code a for loop in Java to find the maximum value in an array
int[] arr = {2,5,3,7,1}; | int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
} | 63,272 |
Create an algorithm in Java to calculate the sum of all elements of an array. | public int calculateSum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
} | 63,325 |
Give an example of a class in Java. | public class Animal {
private String species;
private int age;
public Animal(String species, int age) {
this.species = species;
this.age = age;
}
} | 63,335 |
Design a class in Java that stores information about a person's name, age, and phone number. | public class Person {
private String name;
private int age;
private String phoneNumber;
public Person(String name, int age, String phoneNumber) {
this.name = name;
this.age = age;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public int getAge() {
retu... | 63,360 |
Design a class in Java to represent a bank account. | public class BankAccount {
private double balance;
public BankAccount(double balance) {
this.balance = balance;
}
public void deposit(double depositAmount) {
balance += depositAmount;
}
public void withdraw(double withdrawAmount) {
balance -= withdrawAmount;
}... | 63,371 |
Design a class in Java which allows user to set the value of an Attribute. | public class MyClass {
private int attribute;
public void setAttribute(int attribute) {
this.attribute = attribute;
}
} | 63,390 |
Write a code snippet in Javascript to print out the alphabetical characters in a string.
var myString = "Taichi Tamura" | var myString = "Taichi Tamura";
var letters = "";
for (var i=0; i < myString.length; i++) {
var currentLetter = myString[i];
if (currentLetter.match(/[a-zA-Z]/)) {
letters += currentLetter;
}
}
console.log(letters); // Outputs "TaichiTamura" | 63,408 |
Print out the Fibonacci sequence up to 25 in Java. | int limit = 25;
int[] fib = new int[25];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < limit; i++){
fib[i] = fib[i - 1] + fib[i - 2];
}
for (int i = 0; i < limit; i++){
System.out.println(fib[i]);
} | 63,410 |
Create a program in Java that takes in 2 numbers and swaps them.
int a = 5
int b = 3 | int a = 5;
int b = 3;
int temp = a;
a = b;
b = temp;
System.out.println("a is: " + a); // Outputs 3
System.out.println("b is: " + b); // Outputs 5 | 63,412 |
Create a variable in Java for storing a user's first name. | String firstName; | 63,421 |
Edit the following code to produce a 3x3 multiplication table for the numbers 4 to 6.
for(int i = 1, j = 4; i < 8; i += 2, j++)
System.out.print(i + " × " + j + " = " + i * j + " "); | for(int i = 4; i<7; i++){
for(int j = 1; j<=3; j++){
System.out.print(i + " × " + j + " = " + i * j + " ");
}
System.out.println("");
} | 63,431 |
Modify the following Java code snippet to include an "else if" statement.
int val=20;
if (val > 15)
System.out.println("val is greater than 15"); | int val=20;
if (val > 15)
System.out.println("val is greater than 15");
else if (val == 15)
System.out.println("val is equal to 15"); | 63,465 |
Design a class in Java to represent a rectangle and include methods to calculate and return its perimeter and area. | public class Rectangle {
private int width;
private int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public int getPerimeter() {
return 2 * (width + height);
}
public int getArea() {
return width * height;
... | 63,469 |
Generate a valid Java code for a for loop to count from 5 to 10. | for(int i=5; i<=10; i++) {
System.out.println(i);
} | 63,471 |
Write a code in Java to print the Fibonacci series till a given number. | public static void FibonacciSeries(int limit) {
int prevNum = 0, nextNum = 1;
for (int i = 0; i < limit; i++) {
System.out.print(prevNum + " ");
int sum = prevNum + nextNum;
prevNum = nextNum;
nextNum = sum;
}
} | 63,487 |
Create a Java program that takes an array as argument and then prints out the elements of the array in reversed order.
[1,2,3,4,5] | public class ReverseArray {
/* Prints the array in reverse order */
public static void printReversedArray(int arr[]) {
int n = arr.length;
for (int i=n-1; i>=0; i--) {
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args) {
in... | 63,520 |
Write a Java code to clone a linked list using iteration. | public class LinkedListCloneExample {
static Node head;
static class Node {
int data;
Node next;
Node(int d) {
this.data = d;
this.next = null;
}
}
static Node CloneList(Node node) {
Node curr = node;
Node next = null;
... | 63,542 |
Generate a Fibonacci sequence of 10 numbers using Java. | public static void printFibonacci(int count)
{
int n1 = 0, n2 = 1, n3;
System.out.print(n1 + " " + n2);
// printing fibonacci sequence
for (int i = 2; i < count; ++i)
{
n3 = n1 + n2;
System.out.print(" " + n3);
n1 = n2;
n2 = n3;
}
} | 63,549 |
Write a code to reverse a string in Java. | public class Reverse {
public static void main(String[] args) {
String str = "Hello World";
// Output is reversed string
System.out.println(reverseString(str));
}
public static String reverseString(String str)
{
StringBuilder strBuilder = new StringBuilder();
... | 63,561 |
Write a Java code to generate a Fibonacci number at a given index. | public class Fibonacci {
public static int getFibonacciNumber(int index) {
// base cases
if (index == 0) {
return 0;
}
if (index == 1 || index == 2) {
return 1;
}
// recursive case
return getFibonacciNumber(index - 1) + getFibonacciNumber(index - 2);
}
} | 63,597 |
Come up with Java code to find the square root of a given number.
9 | public double squareRoot(double x) {
double start = 0;
double end = x;
double precision = 0.00001;
while (end - start > precision) {
double mid = (start + end) / 2;
double midSqr = mid * mid;
if (midSqr > x) {
end = mid;
} else {
start = mid;
... | 63,620 |
Complete the following Java code to sort a list of integers.
List<Integer> list = Arrays.asList(5, 2, 8, 1);
// Code to sort | List<Integer> list = Arrays.asList(5, 2, 8, 1);
Collections.sort(list); | 63,627 |
What is an inheritance in Java? | In Java, inheritance is a mechanism in which one class (known as the subclass) inherits the characteristics of another class (known as the superclass). This enables the subclass to reuse the attributes and methods of the superclass without having to code them again. It also allows for increased code modularity and code... | 63,634 |
What is a "Boolean" in Java? | In Java, a Boolean is a data type that can store two possible values - true or false. Boolean types are typically used to represent predicates such as "is the door open?" or "does the user have permission to access this content?". Boolean expressions can be used in decision making, such as if-else statements and multip... | 63,637 |
Write a Java program that finds the first 5 prime numbers, starting from 2. | public class PrimeFinder {
public static void main(String[] args) {
int count = 0;
int num = 1;
while(count < 5) {
num = num + 1;
if (isPrime(num)) {
System.out.println(num);
count++;
}
}
}
public stat... | 63,646 |
Implement a basic calculator in Java. | import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
int number1, number2;
char operation;
double result;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number 1: ");
number1 = scanner.nextInt();
... | 63,656 |
Create a Java program that takes an array of six elements and prints out the sum of the three largest values.
[3, 8, 4, 16, 1, 7] | public class Main {
public static void main(String[] args) {
int[] arr = {3, 8, 4, 16, 1, 7};
int sum = 0;
int largest1 = arr[0];
int largest2 = 0;
int largest3 = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] > largest1) {
largest3 = largest2;
largest2 = largest1;
... | 63,674 |
Create a program in Java to print the Fibonacci number at the end of the given range.
10 | public class Fibonacci {
public static void main(String args[]) {
int n = 10;
int a = 0, b = 1, c;
if (n == 1)
System.out.println("Fibonacci of " + n + " is " + a);
else {
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
... | 63,705 |
Create a program to lowercase all characters of a given string.
HeLLo WoRLd | public class LowerCase{
public static void main(String args[]) {
String str = "HeLLo WoRLd";
System.out.println("Original string: " + str);
System.out.println("Lowercase string: " + str.toLowerCase());
}
} | 63,706 |
Write a Java program to print the nth number in the Fibonacci sequence. | public class FibonacciExample1{
public static void main(String args[]) {
int n = 18, t1 = 0, t2 = 1;
System.out.print("First " + n + " terms: ");
for (int i = 1; i <= n; ++i)
{
System.out.print(t1 + " + ");
int sum = t1 + t2;
... | 63,754 |
Construct an array in Java containing the elements 3, 5, 7, 9. | int[] arr = {3, 5, 7, 9}; | 63,771 |
Write the equivalent Java code for the following Python code.
x = [1, 2, 3, 4]
for x in x:
print(x) | int[] x = {1, 2, 3, 4};
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
} | 63,782 |
Generate a class in Java that validates if a given number is a prime or not.
Number: 13 | public class PrimeNumberValidator {
public static boolean validate(int number) {
if (number < 2) {
return false;
}
for (int i = 2; i < Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
} | 63,791 |
Replace the '?' with the correct Java keyword
public ? myMethod(int a, int b) {
// ...
} | public static myMethod(int a, int b) {
// ...
} | 63,801 |
How do you create a namespace in Java? | In Java a namespace is created by declaring a package statement at the top of a source file. For example: package com.example.myapp; This will create a namespace named "com.example.myapp" and all class and interfaces declared within the source file will be under this namespace. | 63,808 |
Generate a Java program to perform the following task.
Add two integers a and b and print the sum | public class Sum {
public static void main(String[] args) {
int a = 15;
int b = 10;
int sum = a + b;
System.out.println("Sum of a and b is " + sum);
}
} | 63,813 |
Using the given array, write a Javascript program to sort the elements in descending order.
let numbers = [2, 10, 5, 4, 3]; | let numbers = [2, 10, 5, 4, 3];
numbers.sort((a,b) => b - a);
console.log(numbers); //output: [10, 5, 4, 3, 2] | 63,840 |
Write Java code to search for the given element in the list and return its index if it exists.
list = [4, 8, 15, 16, 23, 42], element = 15 | public int findElement (int[] list, int element) {
for (int i = 0; i < list.length; i++) {
if (list[i] == element)
return i;
}
return -1;
} | 63,845 |
Calculate the maximum of three numbers in Java.
int a = 10;
int b = 15;
int c = 20; | int max = Math.max(a, Math.max(b, c));
System.out.println("Maximum of the three numbers is: "+max); | 63,852 |
Generate 20 random numbers between 0 and 5 using Java. | Random random = new Random();
for (int i = 0; i < 20; i++) {
System.out.println(random.nextInt(6));
} | 63,855 |
Write a Java program to calculate the sum of the series 1^3+2^3+3^3+.....+n^3 | public class SumOfSeries {
static int calculateSum(int lastNumber) {
int sum = 0;
for (int i = 1; i <= lastNumber; i++) {
sum = sum + (int)Math.pow(i, 3);
}
return sum;
}
public static void main(String[] args) {
int lastNumber = 4;
... | 63,866 |
Summarize the following Java code.
public class MyClass {
public static void main(String[] args) {
int x = 10;
int y = 20;
int sum = x + y;
System.out.println("The sum of x and y is: " + sum);
}
} | The given Java code declares two int variables (x and y), adds them and prints the sum of the two variables to the console. | 63,876 |
Explain the difference between interface and abstract class in Java. | The main difference between an interface and an abstract class in Java is that an interface can only contain method signatures and constants, whereas an abstract class can have methods, constants and variables. Additionally, an interface cannot have any method implementation, but an abstract class can contain methods w... | 63,882 |
Write a Java program to implement a binary search on a sorted array of integers.
[1, 3, 4, 6, 8, 9], element=6 | // Java Program for Binary Search
public class BinarySearchExample {
// Returns index of x if it is present in arr[], else return -1
int binarySearch(int arr[], int x)
{
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
... | 63,888 |
Suggest code to delete an element from a linked list in Java. | // Function to delete a node in a linked list.
void deleteNode(Node head_ref, Node del)
{
// base case
if (head == null || del == null)
return;
// If node to be deleted is head node
if (head == del)
head = del.next;
// Change next only if node to be
// deleted is ... | 63,902 |
Debug a Java program to correct a compile-time error.
public class Debug {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("Hello");
list.add("World");
}
System.out.println(list);
} | public class Debug {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("Hello");
list.add("World");
for(String str : list){
System.out.println(str);
}
}
} | 63,914 |
Create an if-else statement in Java that prints "Yes!" if an integer is 5 and prints "No!" if an integer is not 5. | if (int == 5) {
System.out.println("Yes!");
}
else {
System.out.println("No!");
} | 63,981 |
Write an if-else statement in Java that checks if a given number is divisible by 3 and prints "Divisible by 3" if true, otherwise "Not divisible". | if (num % 3 == 0) {
System.out.println("Divisible by 3");
}
else {
System.out.println("Not divisible");
} | 63,986 |
Create a class in Java named 'Circle' that has an attribute named 'radius' and a method named 'getArea' to calculate the area of a circle. | class Circle {
double radius;
double getArea() {
return Math.PI * Math.pow(radius, 2);
}
} | 63,991 |
Develop an algorithm in Java for finding all the possible combinations of a given list of numbers.
lst = [1, 2, 3, 4] | public class Combination {
static void combinationUtil(int arr[], int n, int r,
int index, int data[], int i)
{
// Current combination is ready
// to be printed, print it
if (index == r) {
for (int j = 0; j < r; j++)
... | 63,999 |
Rewrite the following Java code using a for loop instead of a for-each loop.
List<Integer> someList = new ArrayList<>();
for (Integer i : someList) {
System.out.println(i);
} | List<Integer> someList = new ArrayList<>();
for (int i=0; i < someList.size(); i++) {
System.out.println(someList.get(i));
} | 64,003 |
Implement a Java class that encapsulates a basic for loop. | public class loop {
public void forLoop(int start, int end){
for(int i=start; i<end; i++){
System.out.println(i);
}
}
} | 64,015 |
Write Java code to calculate the area of a circle with radius 12 cm. | public class AreaOfCircle {
public static void main(String[] args) {
double radius = 12.0;
double area = Math.PI * Math.pow(radius, 2);
System.out.println("Area of circle is: " + area + " cm²");
}
} | 64,021 |
Write Java code to generate random numbers between 0 and 10. | Random rand = new Random();
// Generate random integers in range 0 to 10
int rand_int1 = rand.nextInt(11); | 64,031 |
Create a Java class for car objects. | public class Car {
private String make;
private String model;
private int year;
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
public String getMake() {
return make;
}
public String getModel(... | 64,035 |
Rework the following code so that it prints the same output with fewer lines of code.
int x = 13;
if (x == 13) {
System.out.println("x is 13");
} | int x = 13;
System.out.println(x == 13 ? "x is 13" : ""); | 64,053 |
Suggest an API call in Java to generate a random number | int randomNum = ThreadLocalRandom.current().nextInt(0, MAX_VALUE); | 64,079 |
Generate a code in Java to loop through an array. | for (int i = 0; i < array.length; i++) {
//do something
} | 64,083 |
How would you describe the process of creating a class in Java? | Creating a class in Java involves declaring its name, adding any essential fields and methods, and implementing any desired constructors. The class will then be available for use with other classes in the program. | 64,090 |
Output the most frequent letter in a given string s.
String s = "Hello World!" | String s = "Hello World!"
// Create a HashMap to store frequency of each letter
Map<Character, Integer> freqMap = new HashMap<>();
// Loop through the string
for(char c : s.toCharArray())
{
int freq = freqMap.getOrDefault(c, 0);
// Update the frequency map
freqMap.put(c, freq + 1);
}
// Find the letter with th... | 64,093 |
Divide num1 by num2 using Java.
num1 = 100
num2 = 8 | int result = num1 / num2; // result = 12 | 64,116 |
Create a Java program that prints out the Fibonacci series within a given range
Range = (5, 15) | public class FibonacciRange {
public static void main(String args[]) {
// Set the range
int lowerLimit=5, upperLimit=15;
// Print the fibonacci series
for (int number = lowerLimit; number <= upperLimit; number++) {
System.out.print(fibonacciSeries(number) +" ");
}
}
// Generates the fibonacci se... | 64,123 |
Solve this challenge using Java.
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + n... | class Solution {
public int[] twoSum(int[] nums, int target) {
int [] result = new int[2];
for(int i=0;i<nums.length;i++){
for (int j=i+1;j<nums.length;j++){
if (nums[i]+nums[j]==target){
result[0]=i;
result[1]=j;
... | 64,151 |
Compile and run the following Java code.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
} | <noinput>
Output:
Hello, world! | 64,153 |
Using Java, write a program to check whether an integer is even or odd.
7 | public class OddOrEven {
public static void main(String[] args) {
int num = 7;
// if number is divisible by two then it is even
if (num % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
}
}
# Output:
Odd | 64,231 |
In Java, create a method to detect if a number is a palindrome. | public static boolean isPalindrome(int number) {
int num = number;
int reverse = 0;
while(number > 0)
{
int remainder = number % 10;
reverse = (reverse*10) + remainder;
number = number/10;
}
return num == reverse;
} | 64,253 |
In Java, convert an integer array to a string.
[1, 2, 3, 4] | String str = Arrays.toString(arr); // [1, 2, 3, 4] | 64,262 |
Rewrite the following Java loop as a compact list comprehension.
int[] array = {1, 2, 3, 4, 5};
int[] array2 = new int[array.length];
for(int i=0; i<array.length; i++) {
array2[i] = array[i] + 1;
} | int[] array = {1, 2, 3, 4, 5};
int[] array2 = Arrays.stream(array).map(x -> x + 1).toArray(); | 64,281 |
Identify the variables and operators in the following Java code.
int a = 3 + 4;
int b = a * 2; | Variables:
a, b
Operators:
+, * | 64,282 |
Generate a for loop in Java for printing the squares of each number in a given array.
int[] array = {4, 2, 6, 8}; | for(int x : array) {
System.out.println("Square of " + x + " is " + (x * x));
} | 64,366 |
Find the sum of all even numbers in an integer array using Java.
int[] array = {8, -7, 4, -3, 19, 22}; | int sum = 0;
for (int x : array) {
if (x % 2 == 0)
sum += x;
}
System.out.println("Sum of all even numbers = " + sum); | 64,368 |
Create a class Person in Java that stores a person's name, email, gender, and age. | public class Person {
String name;
String email;
String gender;
int age;
public Person(String name, String email, String gender, int age) {
this.name = name;
this.email = email;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
... | 64,378 |
Use a loop to print out a list of numbers. | for (int i = 0; i < 10; i++) {
System.out.println(i);
} | 64,402 |
Modify the following Java code to reverse a given string.
public class SimpleStringReverser {
public static String reverse(String str) {
String result = ""; | public class SimpleStringReverser {
public static String reverse(String str) {
String result = "";
for(int i = str.length() - 1; i >= 0; i--) {
result += str.charAt(i);
}
return result;
}
} | 64,428 |
Come up with a Java switch statement which will print a different number for various states of a given boolean. | public class ConditionPrinter {
public static void printNumberForCondition(boolean condition) {
switch (condition) {
case true:
System.out.println(1);
break;
case false:
System.out.println(2);
break;
}
}
} | 64,430 |
How would you modify the following Java code to print the first 5 numbers of the Fibonacci sequence?
public class FibonacciPrinter {
public static void printNumbers() {
System.out.println("...");
}
} | public class FibonacciPrinter {
public static void printNumbers() {
int n1=0, n2=1, n3, i, count=5;
System.out.print(n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
} ... | 64,434 |
Create a Java program to validate a credit card number using Luhn's algorithm. | """
Validate a credit card number using Luhn's algorithm.
"""
import java.util.Arrays;
public class Luhn {
public static boolean isValidCard(String cardNumber) {
if (cardNumber == null) {
return false;
}
char checkDigit = cardNumber.charAt(cardNumber.length() - 1);
St... | 64,437 |
Given an array of integers, sort it using bubble sort in Java. | public static int[] bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (array[j] > array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1... | 64,455 |
Create Java classes to represent a user and a post. | public class User {
private String name;
private String email;
//getters and setters
...
}
public class Post {
private int id;
private String title;
private User author;
private String content;
//getters and setters
...
} | 64,464 |
Generate code for an interface class in Java
Name: Calculator
Methods: add(int a, int b)
subtract(int a, int b) | public interface Calculator {
int add(int a, int b);
int subtract(int a, int b);
} | 64,477 |
Create a Java program which takes two integers as input and prints their greatest common divisor.
102, 75 | public static int gcd(int a, int b){
// Base Case
if (a == 0)
return b;
if (b == 0)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
} | 64,482 |
Given the input string, remove extra whitespace from the string using Java.
String s = " This is an example string " | String s = s.replaceAll("\\s+"," ");
System.out.println(s); // This is an example string | 64,513 |
Write a Java program to read the following text and count the number of words in the given sentence.
This is a sample sentence. | import java.util.*;
public class CountWords {
public static void main(String[] args) {
String str = "This is a sample sentence.";
int wordCount = 0;
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) == ' ') || (str.charAt(i) == '.')) {
wordCount++;
}
}
System.out.pri... | 64,529 |
Construct an if-else conditional statement for this situation.
If the number is greater than 0, print "Positive", otherwise print "Negative". | if (number > 0) {
System.out.println("Positive");
} else {
System.out.println("Negative");
} | 64,530 |
Write a Java program to display a message "Hello World" on the console. | class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
} | 64,570 |
Write a Java statement to add two integers and print the result. | int a = 1;
int b = 2;
System.out.println(a+b); | 64,574 |
Develop a program in Java that prints out the following star pattern. | public class Stars {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
} | 64,594 |
Create a Java class to represent a point in 2D space using two fields, x and y. | public class Point {
private double x;
private double y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x =... | 64,612 |
Using Java, create a program to compute the area of a circle given the radius. | """
Using Java, create a program to compute the area of a circle given the radius.
"""
public class AreaCircle {
public static void main(String[] args) {
double radius = 7.5;
double area;
area = Math.PI * Math.pow(radius, 2);
System.out.println("Area of the circle is: " + area);
}
} | 64,625 |
Write a class in Java to submit a form. | public class Form {
public void submitForm(){
// code to submit the form
}
} | 64,642 |
Edit the following Java code snippet to increase efficiency
public class MyClass {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
i++;
System.out.println(i);
}
}
} | public class MyClass {
public static void main(String[] args) {
//Set the counter to 1
int i = 1;
//Use less than or equal comparison to include 10
while (i <= 10) {
//short-hand increment
i++;
System.out.println(i);
}
}
} | 64,674 |
Write a Java program to read input from the console. | import java.util.Scanner;
class UserInput {
public static void main(String[] args)
{
Scanner read = new Scanner(System.in);
System.out.print("Enter some string: ");
String inputString = read.nextLine();
System.out.println("You have entered: " + inputString);
} ... | 64,694 |
Using Java, create a class that implements sorting of an array of integers. | public class Sorting {
public void sort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int index = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[index]) {
index = j; // index of lower number
}
}
int smallerNumber = array[index];
array[index] =... | 64,749 |
Utilizing the Java syntax, write a code snippet to find the maximum value in the given array.
[4, 15, 11, 26, 2] | int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("Maximum value is: " + max); | 64,750 |
Find the subarray with the largest sum in an integer array of length n.
[3, -2, 1, 5, 6, -1, 4] | int[] a = [3, -2, 1, 5, 6, -1, 4];
int max_so_far = Integer.MIN_VALUE;
int max_ending_here = 0;
int start = 0;
int end = 0;
int s = 0;
for (int i = 0; i < a.length; i++) {
max_ending_here += a[i];
if (max_so_far < max_ending_here) {
max_so_far = max_ending_here;
start = s;
end = i;
... | 64,771 |
Insert a node at the beginning of a linked list.
[10 -> 15 -> 20 -> 25] | // Java program to insert a node
// at the beginning of a Singly Linked List
public static void push(Node head_ref, int new_data) {
// allocate node
Node new_node = new Node(new_data);
// put in the data
new_node.data = new_data;
// link the old list off the new node
new_node.next = ... | 64,773 |
Write a Java program to print all the Fibonacci series between 0 to 50. | public class Fibonacci {
public static void main(String args[]) {
int n1=0,n2=1;
System.out.print("Fibonacci Series: " + n1 + " " + n2);
for(int i=2;i<50;i++) {
int n3 = n1 + n2;
System.out.print(" " + n3);
n1=n2;
n2=n3;
}
... | 64,780 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.