Thursday, August 24, 2023

Linear Search in Java

Linear Search in Java

 

public class LinearSearchExample{    

public static int linearSearch(int[] arr, int key){    

        for(int i=0;i<arr.length;i++){    

            if(arr[i] == key){    

                return i;    

            }    

        }    

        return -1;    

    }    

    public static void main(String a[]){    

        int[] a1= {10,20,30,50,70,90};    

        int key = 50;    

        System.out.println(key+" is found at index: "+linearSearch(a1, key));    

    }    

}    


Linear Search in Java (Another way)

public class LinearSearchExample {

    public static int linearSearch(int[] arr, int key) {

        for (int i = 0; i < arr.length; i++) {

            if (arr[i] == key) {

                return i; // Return the index if the key is found

            }

        }

        return -1; // Return -1 if the key is not found

    }


    public static void main(String[] args) {

        int[] a1 = {10, 20, 30, 50, 70, 90};

        int key = 50;

        int resultIndex = linearSearch(a1, key);


        if (resultIndex != -1) {

            System.out.println(key + " is found at index: " + resultIndex);

        } else {

            System.out.println(key + " is not found in the array.");

        }

    }

}


Different Java class examples for practice

  Different Java class examples for practice

Classes  like:

 Student , Rectangle ,  Car , Dog , Book , BankAccount ,  Product ,  Address, Ticket , Team.

1.

class Student {

    String name;

int age;

voiddisplayInfo() {

System.out.println("Name: " + name);

System.out.println("Age: " + age);

    }

}

public class StudentExample {

public static void main(String[] args) {

        Student s = new Student();

        s.name = "John";

s.age = 20;

s.displayInfo();

    }

}

2.

class Rectangle {

double width;

double height;

doublecalculateArea() {

return width * height;

    }

}

public class RectangleExample {

public static void main(String[] args) {

        Rectangle r = new Rectangle();

r.width = 4.0;

r.height = 6.0;

System.out.println("Area of rectangle: " + r.calculateArea());

    }

}

3.

class Car {

    String make;

    String model;

voiddisplayInfo() {

System.out.println("Make: " + make);

System.out.println("Model: " + model);

    }

}

public class CarExample {

public static void main(String[] args) {

        Car car = new Car();

car.make = "Toyota";

car.model = "Corolla";

car.displayInfo();

    }

}

4.

class Dog {

    String name;

    String breed;

voiddisplayInfo() {

System.out.println("Name: " + name);

System.out.println("Breed: " + breed);

    }

}

public class DogExample {

public static void main(String[] args) {

        Dog dog = new Dog();

        dog.name = "Buddy";

dog.breed = "Golden Retriever";

dog.displayInfo();

    }

}

5.

class Book {

    String title;

    String author;

voiddisplayInfo() {

System.out.println("Title: " + title);

System.out.println("Author: " + author);

    }

}

public class BookExample {

public static void main(String[] args) {

        Book book = new Book();

book.title = "The Great Gatsby";

book.author = "F. Scott Fitzgerald";

book.displayInfo();

    }

}

6.

class   BankAccount {

    String accountNumber;

double balance;

voiddisplayBalance() {

System.out.println("Balance: $" + balance);

    }

}

public class BankAccountExample {

public static void main(String[] args) {

BankAccount account = new BankAccount();

account.accountNumber = "123456789";

account.balance = 1000.0;

account.displayBalance();

    }

}

7.

class Product {

    String name;

double price;

voiddisplayInfo() {

System.out.println("Product: " + name);

System.out.println("Price: $" + price);

    }

}

public class ProductExample {

public static void main(String[] args) {

        Product product = new Product();

        product.name = "Laptop";

product.price = 800.0;

product.displayInfo();

    }

}

8.

class Address {

    String street;

    String city;

voiddisplayInfo() {

System.out.println("Street: " + street);

System.out.println("City: " + city);

    }

}

public class AddressExample {

public static void main(String[] args) {

        Address address = new Address();

address.street = "123 Main St";

address.city = "New York";

address.displayInfo();

    }

}

9.

class Ticket {

intticketNumber;

    String eventName;

    String ticketType;

doubleticketPrice;

public Ticket(int number, String event, String type, double price) {

ticketNumber = number;

eventName = event;

ticketType = type;

ticketPrice = price;

    }

public void displayTicketInfo() {

System.out.println("Ticket Number: " + ticketNumber);

System.out.println("Event Name: " + eventName);

System.out.println("Ticket Type: " + ticketType);

System.out.println("Ticket Price: $" + ticketPrice);

    }

}

public class TicketApplication {

public static void main(String[] args) {

        Ticket regularTicket = new Ticket(101, "Concert", "Regular", 50.0);

        Ticket vipTicket = new Ticket(102, "Sports Game", "VIP", 100.0);

System.out.println("Regular Ticket Info:");

regularTicket.displayTicketInfo();

System.out.println();

System.out.println("VIP Ticket Info:");

vipTicket.displayTicketInfo();

    }

}

10.

class Team {

    String teamName;

Player[] players;

Team(String teamName, Player[] players) {

this.teamName = teamName;

this.players = players;

    }

voiddisplayTeamInfo() {

System.out.println("Team Name: " + teamName);

System.out.println("Players:");

for (Player player : players) {

player.displayInfo();

        }

    }

}

Saturday, August 19, 2023

Assignment questions 2023

ASSIGNMENT QUESTIONS 2023
 Unit-1 
1 Program to print the duplicate elements of an array. 
2 Explain String class in Java. 

 Unit-2
1 Explain String Buffer Class Explain String Builder Class 
2 Write a Java program to find the transpose of a Matrix 

 Unit-3
1 List various packages available and its classes in java
2Create packages and add classes to them Implement Class Path for the packages 

 Unit-4 
1 Write a program to sort and reverse the Linked List elements. 
2 Write a program to compare two sets and find the common elements from both sets. 

 Unit-5 
1 Explain about Applet and Advantages, Limitations,
2 Applet Life Cycle How to use parameters in java applet

Machine Learning Course Assignments 2024(Dec)

 Machine Learning Course Assignments 2024 1)   Explain the concept of "generalization" in machine learning. Why is it a central go...