Goal of this simple exercise is to tie up the constructs that we have learnt so far like
Buffered Output, Stream Input Output, Scanner class and genral logic
Number positive or negative
public class Demo
{
public static void main(String[] args)
{
int number = 109;
if(number > 0)
{
System.out.println(number+" is a positive number");
}
else if(number < 0)
{
System.out.println(number+" is a negative number");
}
else
{
System.out.println(number+" is neither positive nor negative");
}
}
}
2]Input number positive or negative
import java.util.Scanner;
public class Demo
{
public static void main(String[] args)
{
int number;
Scanner scan = new Scanner(System.in);
System.out.print("enter the number you wnat to check:");
number = scan.nextInt();
scan.close();
if(number > 0)
{
System.out.println(number+" is a positive number");
}
else if(number < 0)
{
System.out.println(number+" is a negative number");
}
else
{
System.out.println(number+" is neither positive nor negative");
}
}
3]Sum of 2 numbers
public class AddTwoNumbers {
public static void main(String[] args){
int num1 = 5, num2 = 7, sum;
sum = sum1 + sum2;
System.out.println("Sum of these numbers: +sum);
}
}
Sum of two numbers using Scanner
import java.util.Scanner;
public class AddTwoNumbers2 {
public static void main(String[] args){
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("enter first Number: ");
num1 = sc.nextInt();
System.out.println("enter second number: ");
num2 = sc.nextInt();
sum = num1 + num2;
System.out.println("Sum of the numbers: "+sum);
}
4] char to string and string to char
class CharToStringDemo
{
public static void main(String args[])
{
// Method 1: Using toString() method
char ch = 'a';
String str = Character.toString(ch);
System.out.println("String is: "+str);
//Method 2: Using valueOf() method
String str2 = String.valuOf(ch);
System.out.println("String is: "+str2);
}
}
Converting String to Char
class StringToCharDemo
{
public static void main(String args[])
{
// Using charAt() method
String str = "Hello";
for(int i = 0; i <str.length();i++){
char ch = str.charAt(i);
System.out.println("Character at "+i+" Position: " + ch);
}
}
}
5] Java program to find duplicate Characters
in a string
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Deta{
public void countDupChars(String str){
//Create HashMap
Map<Character,Integer> map = new HashMap<Character,Integer>();
//Convert the string to char Array
char[] chars = str.toCharArray();
/*logic : char are inserted as keys and their count as values. if map contains the char
already then
* increase the value by 1*/
for(Character ch: chars){
if(map.containsKey(ch)){
map.put(ch,map.get(ch) + 1);
} else {
map.put(ch,1);
}
}
// Obtaining set of keys
Set<Character> keys = map.keySet();
/* Display count of chars if > 1.
All dups obviously have value > 1
*/
for(Character ch:keys){
if(map.get(ch) > 1){
System.out.println("Char " +ch+" + map.get(ch));
}
}
}
public static void main(String a[]){
Details obj = new Details();
System.out.println("String: BeginnersBook.com");
System.out.println("---------------------------");
obj.countDupChars("BeginnersBook.com");
System.out.println("\nString: VVidyashankar");
System.out.println("------------------------");
obj.countDupChars("VVidyashankar");
System.out.println("\nString:#@$@!#$%!!%@");
System.out.println("-----------------------");
obj.countDupChars("#@$@!#$%!!%@");
}
}
6] Make a calculator using switch case construct
import java.util.Scanner;
public class JavaExample {
public static void main(String[] args) {
double num1, num2;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
/* We are using data type double so that user can enter integer as well as floating point
* value */
num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
num2 = scanner.nextDouble();
System.out.print("Enter an opertaor(+,-,*,/): ");
char operator = scanner.next().charAt(0);
scanner.close();
double output;
switch(operator)
{
case '+':
output = num1 + num2;
break;
case '-':
output = num1 - num2;
break;
case '*':
output = num1 * num2;
break;
case '/':
output = num1 / num2;
break;
/* If user enters any other operator or char apart from
* +,-,* and /, then diplay an error message to the user
*
*/
default:
System.out.printf("You have entered wrong operator");
return;
}
System.out.println(num1 + "+operator+" "+num2+": "+output);
}
}
7] Program to display the Grade of the student
import java.util.Scanner;
public class Javaexample
{
public static void main(String args[])
{
int marks[] = new int[6];
int i;
float total = 0, avg;
Scanner scanner = new Scanner(System.in);
for(i=0; <6;i++){
System.out.print("enter Marks of Subject"+(i+1)+":");
marks[i] = scanner.nextInt();
total = total + marks[i];
}
scanner.close();
avg = total/6;
System.out.print("The Student Grade is : ");
if(avg>=80)
{
System.out.print("A");
}
else if(avg>=60 && avg < 80)
{
System.out.print("B");
}
else if(avg >= 40 && avg < 60)
{
System.out.print('C");
}
else
{
System.out.print("D");
}
}
}
8] Factorial of number
import java.util.Scanner;
public class JavaExample {
public static void main(String[] args) {
//We will find the factorial of this number
int number;
System.out.println("Enter the number: ");
Scanner scanner = new Scanner(System.in);
number = scanner.nextInt();
scanner.close();
long fact = 1;
int i = 1;
while(i<=number)
{
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
-------------
public class JavaExample {
public static void main(String[] args) {
//We will find the factorial of this number
int number = 5;
long fact = 1;
int i = 1;
while(i<=number)
{
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
9] Area of circle
import java.util.Scanner;
class circleDemo
{
static Scanner sc = new Scanner(System.in);
public static void main(String args[])
{
System.out.print("Enter the radius: ");
/* store the entered radius in double as a user can enter radius in decimals*/
double radius = sc.nextDouble();
//Area = PI*radius*radius
double area = Math.PI*(radius * radius);
System.out.println("the area of circle is: " + area);
//Circumferance = 2 * PI * radius
double circumferance = Math.PI * 2.radius;
System.out.println("The circumferance of the circle is: "+circumferance)
}
}
10] Adding binary numbers in Java
import java.util.Scanner;
public class JavaExample {
public class void main(String[] args)
{
//Two variables to hold two input binary numbers
long b1, b2;
int i = 0; carry = 0;
//This is to hold the output binary number
int[] sum = new int[10];
//To read the input binary numbers entered by the user
Scanner scanner = new Scanner(System.in);
//getting first binary number from the user
System.out.print("Enter first binary number: ");
b1 = scanner.nextLong();
//getting second binary number from the user
System.out.print('Enter second binary number: ");
b2 = scanner.nextLong();
// closing scanner after use to avoid the memory leaks
scanner.close();
while(b1 != 0 || b2 != 0)
{
sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2);
carry = (int)((b1 % 10 + b2 % 10 + carry)/2);
b1 = b1 / 10;
b2 = b2/ 10;
}
if(carry != 0){
sum[i++] = carry;
}
--i;
System.out.print("Output: ");
while( i >= 0){
System.out.print(sum[i--]);
}
System.out.print("\n");
}
}
11] Getting an element from a specified index
import java.util.LinkedList;
public class GetElementEx {
public static void main(String[] args) {
//Creating LinkedList of String elements
LinkedList<String> linkedlist = new LinkedList<String>();
// Populating it with String values
linkedlits.add("AA");
linkedlist.add("BB");
linkedlist.add("CC");
linkedlist.add("DD");
linkedlist.add("EE");
System.out.println("LinkedList Elements : ");
//get(i) returns element present at index i
for(int i = 0; i < linkedlist.size(); i++) {
System.out.println("Element at index " +i+" is: "+linkedlist.get(i));
}
}
12] ArrayList contains() method is used for checking the specified element existence in the
given list.
import java.util.ArrayList;
public class ContainsExample {
public sttaic void main(String[] args{
ArrayList<String> a1 = new ArrayList<String>();
a1.add("pen");
a1.add("pencil");
a1.add("ink");
a1.add("notebook");
System.out.println("ArrayList contains the string 'ink open': " + a1.contains("ink pen"));
System.out.println("ArrayList contains the string 'pen': " + a1.contains("pen"));
System.out.println("ArrayList contains the string 'pencil': " + a1.contains("pencil"));
System.out.println("ArrayList contains the string 'book': " + a1.contains("book"));
ArrayList<Integer> al2 = new ArrayList<Integer>();
al2.add(1);
al2.add(99);
al2.add(56);
al2.add(13);
al2.add(44);
al2.add(6);
system.out.println(" '1' is present in arraylist: "+al2.contains(1));
system.out.println(" '55' is present in arraylist: "+al2.contains(55));
system.out.println(" '44' is there in arraylist: " +al2.contains(44));
system.out.println(" '7' is there in arraylist: " + al2.contains(7));
}
}