Posts

Showing posts from July, 2023

Java String Program - Part 1

Image
 Program 1 Write a program in Java to accept a string in lower case and change the first letter of every word to upper case. Display the new string. Sample input: we are in cyber world Sample output: We Are In Cyber World import java.util.Scanner; class P1{          public static void change(){         Scanner sc = new Scanner(System.in);         System.out.println("Enter a sentence");                  String s = sc.nextLine();         String newstr = "";                  s = s.toLowerCase();         s = s.trim();         s = " " + s;                  for(int i=0; i<s.length(); i++){             char ch = s.charAt(i);             if(ch == ' '){      ...

Java - Bubble Sort Numbers and Strings

Image
Bubble sort code for sorting numbers in ascending order import java.util.Scanner; public class Bubble_Asc{     public static void sort_number_asc(){         // input array elements         Scanner sc = new Scanner(System.in);         int a[] = new int[5];         System.out.println("Enter five array elements");         for(int i=0; i<a.length; i++){             a[i] = sc.nextInt();         }                  // sort array elements          for(int i=0; i<a.length; i++){             for(int j=0; j<a.length-1-i; j++){                 if( a[j]>a[j+1] ){                     //swap elements             ...

Java - Class based program

Image
 Program 1 - Eshop import java.util.*; class Eshop {     String name;     double price;          void accept(){         Scanner sc = new Scanner(System.in);         System.out.println("Enter name and price");         name = sc.next();         price = sc.nextDouble();     }          void calculate(){         if (price >= 1000 && price <=25000)             price = price - 0.05 * price;         else if (price >= 25001 && price <= 57000)             price = price - (7.5/100.0) * price;         else if (price >=57001 && price <= 100000)             price = price - (10/100.0) * price;         else      ...