Showing posts with label ap computer science. Show all posts
Showing posts with label ap computer science. Show all posts

Saturday, 15 July 2023

Java - Bubble Sort Numbers and Strings

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
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
        
        
        // output the sorted array
        System.out.println("Sorted array is");
        for(int i=0; i<a.length; i++){
            System.out.print(a[i] + " ");
        }
        
    }
}

Bubble sort code for sorting numbers in descending order

import java.util.Scanner;

public class Bubble_Desc{
    public static void sort_number_desc(){
        // 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
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
        
        
        // output the sorted array
        System.out.println("Sorted array is");
        for(int i=0; i<a.length; i++){
            System.out.print(a[i] + " ");
        }
        
    }
}


Bubble sort code for sorting strings in ascending order

import java.util.Scanner;

public class Bubble_Asc_Strings{
    public static void sort_string_asc(){
        // input array elements
        Scanner sc = new Scanner(System.in);
        String a[] = new String[5];
        
        System.out.println("Enter five array elements");
        
        for(int i=0; i<a.length; i++){
            a[i] = sc.next();
        }
        
        // sort array elements
        for(int i=0; i<a.length; i++){
            for(int j=0; j<a.length-1-i; j++){
                if( a[j].compareTo(a[j+1])>0 ){
                    //swap elements
                    String temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
        
        
        // output the sorted array
        System.out.println("Sorted array is");
        for(int i=0; i<a.length; i++){
            System.out.print(a[i] + " ");
        }
        
    }
}
        
        

Bubble sort code for sorting strings in descending order

import java.util.Scanner;

public class Bubble_Desc_Strings{
    public static void sort_string_desc(){
        // input array elements
        Scanner sc = new Scanner(System.in);
        String a[] = new String[5];
        
        System.out.println("Enter five array elements");
        
        for(int i=0; i<a.length; i++){
            a[i] = sc.next();
        }
        
        // sort array elements
        for(int i=0; i<a.length; i++){
            for(int j=0; j<a.length-1-i; j++){
                if( a[j].compareTo(a[j+1])<0 ){
                    //swap elements
                    String temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
        
        
        // output the sorted array
        System.out.println("Sorted array is");
        for(int i=0; i<a.length; i++){
            System.out.print(a[i] + " ");
        }
        
    }
}

Youtube video link explaining the above code is



 











        
             
                
        

Monday, 10 July 2023

Java - Class based program

 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 
            price = price - (15.0/100.0) * price;
    }
    
    void display(){
        System.out.println("name = " + name);
        System.out.println("net amount to be paid = " + price);
    }
    
    public static void main(){
        Eshop obj = new Eshop();
        obj.accept();
        obj.calculate();
        obj.display();
    }
}

/* Variable Description Table

Variable        Type        Use
name            String      to store name of product
price           double      to store price of product

*/


 Program 2 - CabService



    
import java.util.Scanner;

class CabService{
    // member variables
    
    String car_type;
    double km;
    double bill;
    
    // member methods
    
    CabService(){
        car_type = "";
        km = 0.0;
        bill = 0.0;
    }
    
    void accept(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter car_type and km");
        car_type = sc.nextLine();
        km = sc.nextDouble();
    }
    
    void calculate(){
        if (car_type.equals("AC CAR"))
        {
            if (km<=5){
                bill = 150;
            }
            else {
                bill = 150 + (km - 5) * 10;
            }
        }
        else if (car_type.equals("NON AC CAR"))
        {
            if (km<=5) {
                bill = 120;
            }
            else {
                bill = 120 + (km - 5) * 8;
            }
        }
    }
    
    void display (){
        System.out.println("CAR TYPE: " + car_type);
        System.out.println("KILOMETER TRAVELLED: " + km);
        System.out.println("TOTAL BILL: " + bill);
        
    }
    
    public static void main(){
        CabService obj = new CabService();
        obj.accept();
        obj.calculate();
        obj.display();
    }
}

/*

Variable    Datatype    Usage
car_type    String      used to store cartype
km          double      used to store kilometre
bill        double      used to store bill amount
obj         CabService  object used to invoke methods

*/
      

Program 3 - employee


import java.util.Scanner;
class employee{
    // member variables
    
    int eno;
    String ename;
    int age;
    double basic;
    double net;
    
    // member methods
    
    void accept(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter eno, ename, age, basic salary");
        eno = sc.nextInt();
        ename = sc.next();
        age = sc.nextInt();
        basic = sc.nextDouble();
    }
    
    void calculate(){
        double hra = 18.5/100.0 * basic;
        double da = 17.45/100.0 * basic;
        double pf = 8.10/100.0 * basic;
        net = basic + hra + da - pf;
        // net = basic + (18.5/100.0*basic) + (17.45/100.0 * basic) - (8.10/100.0 * basic);
        if (age > 50)
        {
            net = net + 5000;
        }
    }
    
    void print(){
        System.out.println("eno \t ename \t age \t basic \t net");
        System.out.println(eno + "\t" + ename + "\t" + age +
                           "\t" + basic + "\t" + net );
     }
     
     public static void main(){
         employee obj = new employee();
         obj.accept();
         obj.calculate();
         obj.print();
        }
    }

/* Variable description table

Variable    Datatype    Usage
eno         int         used to store employee no
ename       String      used to store name
age         int         used to store age
basic       double      used to store basic salary
net         double      used to store net salary

*/
         

Explanation of above code can be found at


                    

        

        

        

        

        

        

        

    

  

        
        
    
        
        
        
        
        
        
    
    
    
    
    
    
     
    
    
    
    
    
    
        
        

The cost of legacy technical debt and the need for modernization

 Legacy systems, once the backbone of enterprise IT, are now a major obstacle to innovation, agility, and resilience. Despite the rise of cl...