0% found this document useful (0 votes)
4 views

Java Codes

Java Code i writen

Uploaded by

tempmailfail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Codes

Java Code i writen

Uploaded by

tempmailfail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

//Simple Interest

import java.util.*;

abstract class Interest {


double p;
double r;
double t;

public Interest(double p, double r, double t) {


this.p = p;
this.r = r;
this.t = t;
}

abstract double simpleInterest();


abstract double compoundInterest();
}

class Calculate extends Interest {

public Calculate(double p, double r, double t) {


super(p, r, t);
}

@Override
double simpleInterest() {
return (p * r * t) / 100;
}

@Override
double compoundInterest() {
return p * Math.pow(1 + r / 100, t) - p;
}
}

public class myMain {


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

System.out.println("Enter Principal (P): ");


double p = sc.nextDouble();

System.out.println("Enter Rate of Interest (R): ");


double r = sc.nextDouble();

System.out.println("Enter Time in years (T): ");


double t = sc.nextDouble();

Calculate calc = new Calculate(p, r, t);

double si = calc.simpleInterest();
double ci = calc.compoundInterest();
System.out.printf("Simple Interest is Rs %.2f%n",si);
System.out.printf("Compound Interest is Rs. %.2f%n",ci);
}
}

//Remove double alphabets


import java.util.*;

public class removeMain{


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

System.out.println("Enter the String");


String input = sc.nextLine();

String result = removeDuplicate(input);


System.out.println("Result: " + result);

public static String removeDuplicate(String str){


if (str.length() < 2) return str;
StringBuffer sb = new StringBuffer();
sb.append(str.charAt(0));

for (int i = 1; i < str.length(); i++){


if (str.charAt(i) != str.charAt(i-1)){
sb.append(str.charAt(i));
}
}
return sb.toString();
}
}

//Sort Words
import java.util.Scanner;

public class SortWords {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter words separated by spaces: ");
String input = sc.nextLine();

// Sort and print words using StringBuffer


String sortedWords = sortWords(input);
System.out.println("Sorted words: " + sortedWords);
}

public static String sortWords(String input) {


// Split input into words using space as delimiter
String[] words = input.split(" ");

// Simple bubble sort to sort words


for (int i = 0; i < words.length - 1; i++) {
for (int j = 0; j < words.length - i - 1; j++) {
if (words[j].compareTo(words[j + 1]) > 0) {
// Swap words[j] and words[j + 1]
String temp = words[j];
words[j] = words[j + 1];
words[j + 1] = temp;
}
}
}

// Use StringBuffer to build the final sorted string


StringBuffer sb = new StringBuffer();
for (int i = 0; i < words.length; i++) {
sb.append(words[i]);
if (i < words.length - 1) {
sb.append(" ");
}
}

return sb.toString();
}
}

//Reverse words
public class ReverseWords {
public static void main(String[] args) {
String sentence = "Hello world from Java";
String[] words = sentence.split(" ");

for (int i = words.length - 1; i >= 0; i--) {


System.out.print(words[i] + " ");
}
}
}

//Reverse letter
public class ReverseLetters {
public static void main(String[] args) {
String word = "Java";
for (int i = word.length() - 1; i >= 0; i--) {
System.out.print(word.charAt(i));
}
}
}

You might also like