Write a Perfect Java Coursework with These 10 Exercises

Instant Assignment Help
5 min readJun 15, 2019
Java Coursework

Programming languages and their coursework have always been a dreadful task for students to work on. One such programming language is Java and writing a coursework on the same often calls forth for precision and practice.

To help you with the same, the technical experts have solved 10 basic exercises that will aid in your Java coursework writing task and help you write it perfectly.

Time to learn about these exercises and incorporate them into the coursework…

Exercise 1:
Write “hello” on the screen and print your name in the next line using Java program.

Java Code:
public class Exercise1 {

public static void main(String[] args) {
System.out.println(“Hello\nMarie Clair!”);
}

}

Sample Output:
Hello
Marie Claire!

Exercise 2:
Perform the following mathematical operations in a Java program:
1.Addition of two numbers: 43+15
Java Code:

public class Exercise2 {

public static void main(String[] args) {
System.out.println(43+15);
}

}

Sample Output:
58

2.Division of two numbers: 20/10
Java Code:
public class Exercise2 {

public static void main(String[] args) {
System.out.println(20/10);
}

}

Sample output:
2

Exercise 3:
Find the value of addition, subtraction, multiplication, division, and the remainder of two numbers viz.,
Input first number: 12
Input second number: 26
Java Code:

import java.util.Scanner;

public class Exercise3
{ public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.print(“Input first number: “); int num1 = in.nextInt();

System.out.print(“Input second number: “); int num2 = in.nextInt();

System.out.println(num1 + “ + “ + num2 + “ = “ + (num1 + num2));

System.out.println(num1 + “ — “ + num2 + “ = “ + (num1 — num2));

System.out.println(num1 + “ x “ + num2 + “ = “ + (num1 * num2));

System.out.println(num1 + “ / “ + num2 + “ = “ + (num1 / num2));

System.out.println(num1 + “ mod “ + num2 + “ = “ + (num1 % num2));
}

}

Sample Output:
Input first number: 12
Input second number: 26
12 + 26 = 38
12–26 = -14
12 x 26 = 312
12 / 26 = 0.461
12 mod 26 = 12

Exercise 4:
Give a Java program that will print the area and perimeter of a circle.
Given Data:
Radius of circle= 10
Java Code:

public class Exercise4 {

private static final double radius = 10.0;

public static void main(String[] args) {

double perimeter = 2 * Math.PI * radius;
double area = Math.PI * radius * radius;

System.out.println(“Perimeter is = “ + perimeter); System.out.println(“Area is = “ + area);
}
}

Sample output:
Perimeter is =62.83
Area is = 314.16

Exercise 5:
How to convert a decimal number say, 7 into a binary one, with the help of Java program?
Java Code:

import java.util.Scanner;
public class Exercise5 {
public static void main(String args[])
{
int dec_num, rem, quot, i=1, j;
int bin_num[] = new int[100];
Scanner scan = new Scanner(System.in);

System.out.print(“Input a Decimal Number : “); dec_num = scan.nextInt();

quot = dec_num;

while(quot != 0)
{
bin_num[i++] = quot%2; quot = quot/2;
}

System.out.print(“Binary number is: “);
for(j=i-1; j>0; j — )
{
System.out.print(bin_num[j]);
} System.out.print(“\n”);

}
}

Sample Output:
Input a Decimal Number: 7
Binary number is: 111

Exercise 6:
How to write a Java program that will calculate the number of letters, numbers, spaces, and other characters in an input string?
Given Data:
My Java coursework writing has been made easy 100 times! I Will Practice it more.
Java Code

import java.util.Scanner;
public class Exercise6 {

public static void main(String[] args) {
String test = “My Java coursework writing has been made easy 100 times! I will practice it more.”; count(test);

}
public static void count(String x){
char[] ch = x.toCharArray();
int letter = 0;
int space = 0;
int num = 0;
int other = 0;
for(int i = 0; i < x.length(); i++){
if(Character.isLetter(ch[i])){
letter ++ ;
}
else if(Character.isDigit(ch[i])){
num ++ ;
}
else if(Character.isSpaceChar(ch[i])){
space ++ ;
} else{
other ++;
}
}
System.out.println(“My Java coursework writing has been made easy 100 times! I will practice it more.”);
System.out.println(“letter: “ + letter); System.out.println(“space: “ + space); System.out.println(“number: “ + num); System.out.println(“other: “ + other);
}
}

Sample output:
The string is: My Java coursework writing has been made easy 100 times! I will practice it more.
letter: 62
space: 14
number: 3
other: 2

Exercise 7:
How to write a Java program displaying the screen-time?
Java Code:

public class Exercise7 {

public static void main(String[] args){
System.out.format(“\nCurrent Date time: %tc%n\n”, System.currentTimeMillis());
}
}

Sample Output:
Current Date time: Fri Jun 15 11:11:04 GMT 2019

Exercise 8:
How to rotate an array with a length of 3 integers in the left direction?
Given Array:

[10, 20, 30]

Java Code:
import java.util.Arrays;
public class Exercise8 {
public static void main(String[] args)
{ int[] array_nums = {10, 20, 30}; System.out.println(“Original Array: “+Arrays.toString(array_nums));
int[] new_array_nums = {array_nums[1], array_nums[2], array_nums[0]};
System.out.println(“Rotated Array: “+Arrays.toString(new_array_nums));
}
}

Sample Output:
Rotated Array: [20, 30, 10]

Exercise 9:
How to check whether a string starts with a specified word or not in a Java program?

Given Data:
String 1: “It is a beautiful day”

Java Code:
import java.util.*;
import java.io.*;
public class Exercise9 {
public static void main(String[] args)
{
String string1 = “It is a beautiful day.”; System.out.println(string1.startsWith(“It”));
}
}

Sample Output:
true

Exercise 10:
How to find odd elements from a given list of integers in a Java Program?

Given Data
[5, 8, 7, 9, 4, 6]

Java Code:
import java.util.*;
public class Exercise10 {
public static void main(String[] args)
{
int[] nums = {5, 8, 7, 9, 4, 6};
int ctr_even = 0, ctr_odd = 0;
System.out.println(“Original Array: “+Arrays.toString(nums));

for(int i = 0; i < nums.length; i++) {
if(nums[i] % 2 == 0)
{
ctr_even++;
}
else
ctr_odd++;
}
System.out.printf(“\nNumber of even elements in the array: %d”,ctr_even);
System.out.printf(“\nNumber of odd elements in the array: %d”,ctr_odd);
System.out.printf(“\n”);
}
}

Sample Output:

Original Array: 5, 8, 7, 9, 4, 6


Number of even elements in the array: 3
Number of odd elements in the array: 3

Now that you know about the basic exercises that are at the heart of Java, it is time to start with your Java coursework writing real quick! But, make sure not hustle on the same as precision will lead to perfection.

Summary: Writing a Java coursework may not be an easy task for students, therefore to help them with the same, this article gives solution to the 10 basic Java programming exercises.

--

--