Posts

Showing posts from July, 2024

KTU OOP LAB JAVA CSL 203 BTech CS S3 - Dr Binu V P

About Me Scheme and Evaluation Syllabus and Practice Questions ******************************** How to do Java Programs in  Debian PART A Basic Java Programs Using Control  Statements 1.String Problems 2.Numerical Problems 3.Arrays 4.Matrix Lab Exercise Java-1 PART B  Object Oriented Programming 1.Creating classes and objects. constructor for initializing object 2.Method overloading in Java 3.Single Inheritance 4.Multilevel Inheritance 5.Method overriding in Java 6. Hierarchical Inheritance 7.Interface in Java 8.Abstract classes in java  PART C Exception and File Handling 1.Exception Handling in Java 2.Reading and Writing Text Files 3.Buffered Reader and String Tokenizer PART D Multi-Threading  Creating Threads in Java Thread Synchronization in Java Thread Priorities in Java Programs using Multi-Threading PART E Graphics and Database Programming Introduction to Java Swing Drawing Shapes Drawing  a Sine Wave Handling Mouse Events Handling Keyboard Events Drawing Shapes with Mouse Events

KTU OOP LAB JAVA CSL 203 Scheme and Evaluation

Preamble: The aim of the course is to provide hands-on experience to the learners on various object oriented concepts in Java Programming. This course helps the learners to enhance the capability to design and implement various Java applications for real world problems. Prerequisite: Topics covered under the course Programming in C (EST 102) Course Outcomes: At the end of the course, the student should be able to CO1:Implement the Object Oriented concepts - constructors, inheritance, method overloading & overriding and polymorphism in Java (Cognitive Knowledge Level:Apply) CO2:Implement programs in Java which use datatypes, operators, control statements, built in packages & interfaces, Input/Output streams and Files (Cognitive Knowledge Level: Apply) CO3:Implement robust application programs in Java using exception handling (Cognitive Knowledge Level: Apply) CO4 :Implement application programs in Java using multithreading and database connectivity (Cognitive Knowledge Level: A

String Problems

Reverse a String import java.util.Scanner; public class ReverseString { public static void main(String[] args) {      Scanner scanner = new Scanner(System.in);      System.out.print("Enter a string to reverse: ");      String input = scanner.nextLine();      String reversed = new StringBuilder(input).reverse().toString();      System.out.println("Reversed string: " + reversed);      scanner.close(); } } without using StringBuilder import java.util.Scanner; public class ReverseString {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         System.out.print("Enter a string to reverse: ");         String input = scanner.nextLine();         // Manually reverse the string         String reversed = "";         for (int i = input.length() - 1; i >= 0; i--) {             reversed += input.charAt(i);         }         System.out.println("Reversed string: " + reversed);                  scanner.c

How to do Java Programs in Debian

Image
  1. Install Java Development Kit (JDK) First, ensure you have the JDK installed. You can install it using the package manager: sudo apt update sudo apt install default-jdk 2. Verify the Installation Check if Java is installed correctly: java -version 3. Write the Java Program Create a directory for your Java program and navigate to it:sh mkdir  /java_programs cd  /java_programs Create a file named HelloWorld.java using your preferred text editor (e.g., nano, vim,edit ) gedit HelloWorld.java Add the following code to the file: public class HelloWorld {   public static void main(String[] args)  {   System.out.println("Hello, World!");   }  } Save the file and exit the text editor. 4. Compile the Java Program Compile the Java program using the javac command: javac HelloWorld.java This will generate a file named HelloWorld.class. 5. Run the Java Program Run the compiled Java program using the java command: java HelloWorld You should see the output: Hello, World! Naming Conventio

Syllabus and Practice Questions KTU OOPS Lab Java CSL 203

SYLLABUS The syllabus contains six sessions (A, B, C, D, E, F). Each session consists of three concrete Java exercises, out of which at least two questions are mandatory. (A) Basic programs using datatypes, operators, and control statements in Java. 1) Write a Java program that checks whether a given string is a palindrome or not. Ex: MALAYALAM is palindrome. 2) Write a Java Program to find the frequency of a given character in a string. ** 3) Write a Java program to multiply two given matrices. ** (B) Object Oriented Programming Concepts: Problem on the use of constructors, inheritance,  method overloading & overriding, polymorphism and garbage collection: 4) Write a Java program which creates a class named 'Employee' having the following  members: Name, Age, Phone number, Address, Salary. It also has a method named 'print-Salary( )' which prints the salary of the Employee. Two classes 'Officer' and 'Manager' inherits the 'Employee' class. T

Arrays

Find the max element in an array import java.util.Scanner; public class FindMaxInArray {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         System.out.print("Enter the number of elements in the array: ");         int n = scanner.nextInt();         int[] numbers = new int[n];         System.out.println("Enter the elements of the array:");         for (int i = 0; i < n; i++) {             numbers[i] = scanner.nextInt();         }         scanner.close();         int max = numbers[0];         for (int i = 1; i < numbers.length; i++) {             if (numbers[i] > max) {                 max = numbers[i];             }         }         System.out.println("Maximum number in the array: " + max);     } } ************************************************************ Reversing an Array import java.util.Scanner; public class ReverseArray {     public static void main(String[] args) {         Scanner scanner

Numerical Problems

Find Roots of a quadratic equation import java.util.Scanner; public class QuadraticEquation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the coefficient a: "); double a = scanner.nextDouble(); System.out.print("Enter the coefficient b: "); double b = scanner.nextDouble(); System.out.print("Enter the coefficient c: "); double c = scanner.nextDouble(); double discriminant = b * b - 4 * a * c; double root1, root2; if (discriminant > 0) {      root1 = (-b + Math.sqrt(discriminant)) / (2 * a);      root2 = (-b - Math.sqrt(discriminant)) / (2 * a);      System.out.println("The roots are real and different.");      System.out.println("Root 1: " + root1);      System.out.println("Root 2: " + root2);         } else if (discriminant == 0) { root1 = root2 = -b / (2 * a); System.out.println("The roots are real and the same."); System.out.println("Root 1 = Ro

Matrix

Reading and Printing Matrix import java.util.Scanner; public class MatrixInput {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         // Reading number of rows and columns         System.out.println("Enter the number of rows:");           int rows = scanner.nextInt();           System.out.println("Enter the number of columns:");          int columns = scanner.nextInt();         // Initializing the matrix         int[][] matrix = new int[rows][columns];         // Reading the matrix elements         System.out.println("Enter the elements of the matrix:");         for (int i = 0; i < rows; i++) {             for (int j = 0; j < columns; j++) {                 matrix[i][j] = scanner.nextInt();             }         }         // Displaying the matrix         System.out.println("The matrix is:");         for (int i = 0; i < rows; i++) {             for (int j = 0; j < columns; j++) {      

Lab Exercise Java -1

1.Read a string and print the words in Sorted order. 2.Insert a string at a particular position in another string Example:Input:Model College  string to insert:Engineering pos:5 Output:Model Engineering College 3.Remove Duplicates Description: Given a string, remove the duplicate characters in it. Example: Input: "aabbcc" Output: "abc" 4.Reverse Words in a String Description: Given an input string s, reverse the order of the words. Example: Input: "the sky is blue" Output: "blue is sky the" 5.Find the largest digit in a number also print all its positions Input:3586708 largest digit is 8 occurs at position 3,8 6.Convert the given decimal number into binary Input;13 Output;1101 7.Find the mean,median and mode of set of data 8.Given two sorted arrays A and B with unique elements. create a new array C in sorted order by merging A and B A=[5,8,9,10] B=[3,6,7] C=[3,5,6,7,8,9,10] 9.A saddle point in a matrix is an element that is the smallest in its ro

Classes and objects-Employee class

Let's break down the concepts of classes, objects, and constructors in Java with an example. Classes in Java A class in Java is a blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into one single unit. A class can contain fields (variables) and methods to describe the behavior of an object. Objects in Java An object is an instance of a class. When a class is defined, no memory is allocated until an object of that class is created. Objects are created using the new keyword. Constructors in Java A constructor in Java is a special method that is called when an object is instantiated. Its primary purpose is to initialize the newly created object.  A constructor: Has the same name as the class. Does not have a return type. Can be overloaded to accept different numbers or types of parameters. Example Let's create a simple Employee class to illustrate these concepts. class Employee {     // Member variables     private String nam

Method Overloading in Java

Method overloading in Java is a feature that allows a class to have more than one method with the same name, provided their parameter lists (also known as method signatures) are different. This is a type of polymorphism (specifically, compile-time or static polymorphism) where multiple methods can perform similar but slightly varied tasks, differentiated by the number and type of their parameters. Key Points of Method Overloading: Same Method Name : The overloaded methods must have the same name. Different Parameter List : The parameter list must differ in at least one of the following ways: Number of parameters Type of parameters Order of parameters (if the types are different) Return Type : The return type can be different, but it does not contribute to the method signature for overloading purposes. Benefits of Method Overloading: Readability : Makes the code easier to read and understand by using the same method name for similar actions. Reusability : Encourages code reuse by allowi

Inheritance - Single Inheritance

Inheritance is a fundamental concept in object-oriented programming that allows one class to inherit properties and methods from another class. This mechanism promotes code reusability and establishes a relationship between the parent class (superclass) and the child class (subclass). In single inheritance, a class inherits from only one superclass. This is the most basic form of inheritance. Here's a simple Java program to demonstrate single inheritance: // Superclass class Animal {     // Method in the superclass     void eat() {         System.out.println("This animal eats food.");     } } // Subclass class Dog extends Animal {     // Method in the subclass     void bark() {         System.out.println("The dog barks.");     } } // Main class to test the inheritance public class Main {     public static void main(String[] args) {         // Create an object of the subclass         Dog myDog = new Dog();                  // Call the methods of the superclass an

Hierarchical Inheritance

Hierarchical inheritance in Java is a type of inheritance where multiple subclasses inherit from a single superclass. This type of inheritance allows a single class to serve as a base class for multiple derived classes, promoting code reuse and consistency across related classes. Example of Hierarchical Inheritance in Java Let's create an example with a superclass called Animal and three subclasses: Dog, Cat, and Bird. Each subclass will inherit from the Animal superclass and add specific behavior. // Superclass class Animal {     String name;     // Constructor     Animal(String name) {         this.name = name;     }     // Method to display the name     void display() {         System.out.println("Animal Name: " + name);     }     // Method to make a sound (to be overridden by subclasses)     void sound() {         System.out.println("Animal makes a sound");     } } // Subclass Dog class Dog extends Animal {     // Constructor     Dog(String name) {         s