0% found this document useful (0 votes)
3 views2 pages

PPL COPY IMP

Uploaded by

aryadusane12
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)
3 views2 pages

PPL COPY IMP

Uploaded by

aryadusane12
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/ 2

Addition of matrix Switch case multiple & division Definition: The constructor is a specialized method for initializing objects.

import java.util.Scanner; import java.util.Scanner; Name of the constructor is same as that of its class name. In other words, the name of the
public class MatrixAddition { constructor and class name is same.
public class ArithmeticOperations {
public static void main(String[] args) { Class Test
Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Constructor
// Get the dimensions of the matrices int rows = scanner.nextInt(); Scanner scanner = new Scanner(System.in);
Class Name
int columns = scanner.nextInt(); System.out.println("Enter two numbers: "); int number1 = scanner.nextInt(); Whenever an object of its associated class is created, the constructor is invoked automatically.
// Create two matrices int number2 = scanner.nextInt(); • The constructor is called constructor because it creates values for data fields of class
int[][] matrix1 = new int[rows][columns];int[][] matrix2 = new int[rows] System.out.println("Enter the operation you want to perform: "); System.out.println("1. Types of Constructor:
[columns];
Addition");System.out.println("2. Subtraction"); System.out.println("3. Multiplication"); Various types of constructors are-
// Read the elements of the matrices for (int i = 0; i < rows; i++) {
System.out.println("4. Division"); 1. No argument(Default) constructor 2. Parameterized constructor
for (int j = 0; j < columns; j++) { matrix1[i][j] = scanner.nextInt();
int operation = scanner.nextInt(); 3. Copy constructor
}}
int result = 0; switch (operation) { 1. No argument constructor: The default constructor is a constructor for which there are no
for (int i = 0; i < rows; i++) {
arguments or parameters passed. For examples
for (int j = 0; j < matrix2[i][j] = }
Parameterized constructor: A parameterized constructor is a constructor that has one or more
}} case 1: result
// Add the two matrices
parameters. The parameters are used to initialize the state of the object when it is created.
break; case 2:  Copy constructor: A copy constructor is a constructor that takes an object of the same class
int[][] resultantMatrix
result as a parameter and uses it to initialize the state of the new object.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) { break; case 3: / Default constructor public class Person { public Person() {
resultantMatrix[i][j] = matrix1[i][j] + matrix2[i][j]; result // Initialize the state of the object }
}} break; case 4: }
// Set the diagonal elements of the resultant matrix to 0 for (int i = 0; i < rows; result // Parameterized constructor public class Circle {
i++) { private double radius;
break; default:
resultantMatrix[i][i] = 0; } public Circle(double radius) { this.radius = radius;
= number1 + number2; = number1 - number2; = number1 * number2; = number1 /
// Print the resultant matrix for (int i = 0; i < rows; i++) { }}
for (int j = 0; j < columns; j++) { System.out.print(resultantMatrix[i][j] + " "); number2; // Copy constructor public class Point { private int x, y;
} System.out.println("Invalid operation."); break; public Point(Point point) { x = point.x;
System.out.println(); } } y = point.y; }
columns; j++) { scanner.nextInt(); System.out.println("The result is: " + result); }}} }
= new int[rows][columns } } }

PRIMITIVES DATA Java Virtual Machine is a set of software and program components. As the name suggests, it is a virtual Q.1 Justify the meaning of each characteristic of Java in the statement "Java is simple,
Various data types used in Java are byte, short, int, long, char, float, computer that resides in the real computer. Java can achieve its platform independence feature due to Java architecture neutral portable. interpreted and robust and secured programming language".
Virtual Machine.
double and boolean. Ans.: i) Java is simple:Java is a simple programming language. Even though you have no
• The execution of any Java program is carried out as shown in Fig
byte: This is in fact smallest integer type of data type. Its width is of programming background you can learn this language comfortably. The programmers who worked
When we want to write any Java program, we write the source code and store it in a file with extension java.
8-bits with the range 128 to 127. The variable can be declared as . This java file is compiled using javac and a class file gets created. This class file is actually a byte code. The on C or C can learn this language more efficiently because the syntax of Java resembles with C and
byte type as byte i,j name byte code is given because of the instruction set of Java program. C++ ii) Architecture neutral portable: Platform independence means programs in Java can be
This data type is also used for defining the signed numerical The Java Virtual Machine takes byte code as an input, reads it. executed on variety of platforms.Java supports portability in 2 ways Java compiler generates the
interprets it and then executes it. byte code which can be further used to obtain the corresponding
variables with a width of 16-bits and having a range from- 32,768 to
• The Java Virtual machine can generate an output corresponding to machine code. Secondly the primitive data types used in Java are
32,767. The variable can be declared as short as short a.b the underlying platform (operating system).
int: This is the most commonly used data type for defining the machine independent.
. Java does not support the concept of pointers directly. This makes it impossible to accidentally reference
numerical data. The width of this data type is 32-bit having a range memory that belongs to other programs or the kernel.
iii) Interpreted: Normally programming languages can be either compiled or interpreted but Java is
2,147,483,648 to 2,147,483,647. The declaration can be int p.q: a language which can be compiled as well as interpreted. First, Java compiler translates the Java
long: Sometimes when int is not sufficient for declaring some data 2. The output of Java compiler is not executable code but it is a bytecode. The instructions of bytecode is source program into a special code called bytecode. Then Java interpreter interprets this
then long is used. The range of long is really very long and it is-9,
executed by the Java Virtual Machine (JVM). That means JVM converts the bytecode to machine readable form. bytecode to obtain the equivalent machine code. This machine code is then directly executed to
JVM understand only . obtain the output.
223. 372, 036, 854, 775, 808 to 9. 223, 372, 036, 854, 775, 807. The Java is a platform independent and portable programming language Platform independence is the most
iv) Robust and Secure: Following are reasons that make Java more secured than other
declaration can be long x.y. exciting feature of Java program. That means
programming languages.
float: To represent the real number(i.e. number that may have programs in Java can be executed on variety of platforms. This feature is based on the goal-write once, run
anywhere and at anytime forever. Java supports portability in 2 ways - Java compiler generates the byte code 1. Java does not support the concept of pointers directly. This makes it impossible to accidentally
decimal point) float data type can be used. The width is 32-bit and
which can be further used to obtain the corresponding machine code. Secondly the primitive data reference memory that belongs to
range of this data type is 1.4e-045 to 3.4e+038.
types used in Java are machine independent. other programs or the kernel.
double: To represent the real numbers of large range the double 4. Java is known as object oriented programming language 2 The output of Java compiler is not executable code but it is a bytecode. The instructions of
data type is used. Its width is 64-bit having the range 4.9e324 to Java is popularly recognised as an object oriented programming language. It supports various object oriented bytecode is executed by the Java Virtual Machine (JVM). That means JVM converts the bytecode
1.8e+ 308. features such as data encapsulation, inheritance, polymorphism and dynamic binding. Everything in Java is an
to machine readable form. JVM understand only the bytecode and therefore any infectious code
char: This data type is used to represent the character type of data. object. The object oriented model of Java is simple and can be extended easily,
5. Java is multithreaded and interactive language Java supports multithreaded programming which allows a
can not be executed by JVM. No virus can infect bytecode. Hence it is difficult to trap the internet
The width of this data type is 16-bit and its range is 0 to 65,536. Let
programmer to write such and network based applications by the hackers.
us see one simple Java program which makes use of various data 3. In programming languages like C or C++ the memory management is done explicitly by the user.
a program that can perform many tasks simultaneously. This ultimately helps the
types. boolean: Boolean is a simple data type which denotes a value developer to develop more interactive code. Java can be compiled and interpreted That means user allocates or deallocates the memory. Whereas in Java its automatically done
to be either true or false. Normally programming languages can be either compiled or interpreted but Java is a language which can be using garbage collection. Thus user can not perform the memory management directly.
compiled as well as interpreted. First, Java compiler translates the Java source program into a special code If an applet is executing in some browser then it is not allowed to access the file system of local
called bytecode. Then Java interpreter interprets this bytecode to obtain the equivalent machine code. This
Multitasking -Multi threading machine.
machine code is then directly executed to obtain the output

Q5) a) Write short notes on React JS. Enlist and explain the feature of it. AngularJS is a JavaScript framework for building single- LOGIN PAGE.
React JS is a JavaScript library for building user interfaces. It is used to
create interactive web applications that are both fast and scalable. React
page applications. It was developed by Google and is one <html>
JS is based on the concept of components, which are reusable pieces of of the most popular JavaScript frameworks in use today. <head>
code that can be combined to create complex UIs. Advantages of AngularJS <title >LOGIN FORM DEMO< /title>
Features of React JS:
 Single-page applications: AngularJS makes it easy to </head>
 JSX: JSX is a JavaScript syntax extension that allows you to write HTML-
like code in JavaScript. This makes it easier to create and maintain React build single-page applications, which are applications <body bgcolor="khaki's
components. that load all of their content in a single HTML page. This <center>
 Virtual DOM: The virtual DOM is a lightweight representation of the real <h2>Login Form</h2> </center>
can improve performance and user experience.
DOM. React JS uses the virtual DOM to track changes to the UI and only
 Data binding: AngularJS provides two-way data <form name= "form1">
updates the real DOM when necessary. This makes React JS very efficient.
 One-way data binding: React JS uses one-way data binding, which means binding, which means that changes to the model are <table> <tr>
that data flows from the parent component to the child component, but automatically reflected in the view, and vice versa. This Std> <b> Name: </b> </ta>
not the other way around. This makes it easier to reason about the state of <td> <input type= "text" name=
your application and prevents errors.
can make it easier to build interactive and responsive
applications. "userName"> </tr>
 Performance: React JS is known for its excellent performance. This is due
to the use of the virtual DOM and one-way data binding. <tr>
 Dependency injection: AngularJS uses dependency
 SEO friendly: React JS applications are SEO friendly. This means that < t d > < b >Password: </b> </td>
they can be indexed by search engines and appear in search results.
injection, which is a design pattern that allows you to
<td> <input type
 Large community: React JS has a large and active community. This decouple your code and make it easier to test.
="password"name="owd"> </td>
means that there are many resources available to help you learn React JS  Reusable components: AngularJS provides a way to
and troubleshoot problems. </tr>
create reusable components, which can make it easier to
Advantages of React JS: <tr>
 Fast and efficient: React JS is very fast and efficient, making it ideal for build complex applications. <td> <input type="submit" name
building large and complex applications. Disadvantages of AngularJS ="submit".
 Easy to learn: React JS is relatively easy to learn, even for beginners.
 Complexity: AngularJS can be a complex framework, value ="Submit"> < / t d >
 Declarative: React JS is declarative, which means that you can describe
what you want which can make it difficult to learn and use. <td> <input type="reset" name = "reset"
the UI to look like, rather than how to make it look that way. This makes it  Performance: AngularJS can have a negative impact on value ="Reset"> </td>
easier to performance, especially for large applications </tr> </table> </form>
reason about the code and makes it easier to test.
 Reusable components: React JS components are reusable, which makes Security: AngularJS applications are not inherently </body>
it easy to build complex UIs from smaller, simpler components. secure, and you need to take steps to secure them. <htmi>

c) State the term thread synchronization. Explain how to achieve thread Features of java script. LISP: LISP is oriented towards
synchronization in Java. Object-oriented programming: JavaScript c) Explain the basic list manipulation in
programming at terminal with quick
 Synchronized blocks: A synchronized block is a block of code that can is an object-oriented programming prolog./EXPAIN PROLOG
only be executed by one thread at a time. To create a synchronized response.
language, which means that it uses objects Appending a list: The | operator is used to
block, you use the synchronized keyword on the object that you want to • LISP can be better used with editing and
to represent data and behavior. This
lock. For example:
makes JavaScript code more organized debugging tools. o LISP si basicaly list append a list to another list. For example, the
Code snippet
and reusable. Dynamic typing: JavaScript processing language, hence ti can be very following code appends the list [1, 2, 3] to the
synchronized (object) {// This block of code can only be executed by one
is a dynamically typed language, which easily used for symbol manipulation. list [4, 5, 6] to create the list [1, 2, 3, 4, 5, 6].
thread at a time. }
 Synchronized methods: A synchronized method is a method that can means that the type of a variable can • The function and data form of LISP si very Prolog
only be executed by one thread at a time. To declare a method as change at runtime. This makes JavaScript much similar. Hence one LISP function can
code more flexible, but it can also make it
append([1, 2, 3], [4, 5, 6], L).
synchronized, you use the synchronized keyword on the method be used to analyze another.
declaration. For example: more difficult to debug. (defun factorial (n) (cond ((= n 0) 1) ((= n 1) 1)  Length of a list: The length/2 predicate is used
Code snippet  Event-driven programming: JavaScript is (t (* n (factorial (- n 1)))))) (print (factorial 5)) to find the length of a list. For example, the
public synchronized void myMethod() { an event-driven language, which means
// This method can only be executed by one thread at a time.
This program defines a function called following code finds the length of the list [1, 2, 3]
that it responds to events that occur in the
} browser. This makes JavaScript code more
factorial that takes an integer n as input and and stores the result in the variable L. Prolog
Thread synchronization is important for ensuring that shared resources
responsive and interactive.
returns its factorial. The function uses length([1, 2, 3], L).
are accessed safely and consistently. Without thread synchronization, it recursion to calculate the factorial. The
is possible for multiple threads to corrupt shared data or race conditions
 Cross-platform compatibility: JavaScript  Member of a list: The member/2 predicate is
is a cross-platform language, which means base case is when n is 0 or 1, in which case
to occur. used to check if an element is a member of a list.
that it can be run on different browsers the factorial is 1. Otherwise, the factorial is
Here are some of the benefits of thread synchronization in Java:
and operating systems. This makes equal to n multiplied by the factorial of n-1. For example, the following code checks if the
 Prevents data corruption: Thread synchronization prevents multiple
threads from corrupting shared data. This is because only one thread can JavaScript a popular choice for web To run the program, you can save it as a file element 2 is a member of the list [1, 2, 3].
access the shared data at a time. development. called factorial.lisp and then run it from the Prolog
 Avoids race conditions: Race conditions occur when two or more Here is a JavaScript program to display a command line using the clisp command. For
threads are trying to access the same shared resource at the same time.
member(2, [1, 2, 3]).
welcome message:
example, if you are on a Unix-like operating  Finding the maximum element in a list: The
Thread synchronization can help to avoid race conditions by ensuring JavaScript
system, you would run the program as
that only one thread can access the shared resource at a time. function welcome() { alert("Welcome to following code finds the maximum element in
 Improves performance: Thread synchronization can improve follows:
JavaScript!"); the list [1, 2, 3, 4, 5] and stores the result in the
performance by preventing threads from having to wait for each other to
} Code snippet
access shared resources. clisp factorial.lisp variable Max.
welcome();
Here are some of the drawbacks of thread synchronization in Java: Prolog
This will print the factorial of 5, which is 120.
 Can reduce performance: Thread synchronization can reduce
performance if it is used too heavily. This is because threads have to wait max_element([1, 2, 3, 4, 5], Max).
for each other to release locks before they can access shared resources.
 Can be complex to implement: Thread synchronization can be complex
to implement, especially in large and complex applications.
Q.1 Describe functional programming. Enlist its features. Also Describe logical programming. Enlist its features. Also list the a) Define Inheritance and list the advantages. Explain the types of Inheritance in Java with suitable Java code
list the commonly used functional programming languages. commonly used logical programming languages. a) Define Inheritance and list the advantages.
Ans.: Functional languages are those languages in which the basic Inheritance is a mechanism in Java by which one class can inherit the properties and behaviors of another class.
Ans.: Logic programming is a programming paradigm in which the set
building block is functions. This means that the child class can reuse the code of the parent class, which can save time and effort in
In functional programming the programmer is concerned only of sentences are written in logical form. The logic programs consists development.Here are some of the advantages of inheritance:
with functionality and not memory related variable storage and of facts and rules about some problem demain  Code reusability: Inheritance allows you to reuse the code of existing classes, which can save you a lot of time
assignment sequence. Logical and Functional Programming and effort in development.
Features of Functional Programming Language Logic programming can be viewed as controlled deduction.  Abstraction: Inheritance can help you to abstract the details of a class, which can make your code more
1. The functional programming languages are modelled on the modular and easier to understand.
An important concept in logic programming is the separation of
concept of mathematical functions and make use of only  Polymorphism: Inheritance can be used to achieve polymorphism, which allows you to treat objects of
conditional expressions and recursion for the computational
programs into their logic component and their control component. . different types in a similar way.
purpose. Kowalski illustrates the logic programming by following equation Types of Inheritance in Java:
2. Le programs are constructed by building functional Algorithm-Logic+ Control There are three types of inheritance in Java:
applications. That means, the values produced by one or more where "Logic" represents a logic program in the form of facts and  Single inheritance: A class can inherit from only one parent class.
functions become the parameters to another functions  Multilevel inheritance: A class can inherit from multiple parent classes, in a hierarchical
rules. The "Control" represents how the algorithm can be
3. The purest form of functional programming languages use fashion.
neither sariables nor assignment statements, although this is
implemented by applying the rules in particular order.  Hierarchical inheritance: A class can inherit from a parent class, and that parent class
relaxed somewhat in most applied functional languages The concept of logic programming is linked up with a language called can inherit from another parent class, and so on. Java code example:
4. The functional languages can be categorized in two types - prolog. Prolog is acronym of PROgramming in LOGIC Features of class Animal
Pure functional languages and impure functional languages. Prolog {
a. The pure functional language support only functional String name; void speak() {
1) Prolog is used in artificial intelligence and expert system to solve
paradigm. Example of pure functional language is ML... Haskell. System.out.println("I am an animal"); }
complex problems. }
b. The impure functional language can be used to write
imperative style programs/For example C is a imperative style 2) It is used on pattern matching algorithm. class Dog extends Animal { String breed;
programming language). Example of impure functional 3) It is used to develop systems based on object oriented concepts. void speak() {
programming language is LISP.Commonly used Functional 4) Prolog is used to build decision support systems that aid System.out.println("Woof!"); }
Programming Languages .LISP, Sisal, Haskell, ML, APL, Scheme }
organizations in decision-making For example - decision systems for
are some commonly used functional language public class Main {
medical diagnoses public static void main(String[] args) {
Dog dog = new Dog("Spot", "Golden Retriever");
dog.speak(); }
}

Describe Exception. Write any two examples of exception. Explain abstract classes and polymorphism in Java with appropriate java codes. Creating a package code
Explain keywords try, catch, throw, throws and finally related to Abstract classes
An abstract class is a class that cannot be instantiated. It is a blueprint for other classes to inherit from.
To create a package, you need to add the package statement at the
exception handling
Abstract classes can contain abstract methods, which are methods that have no implementation. The beginning of your Java source file. The package statement specifies the
here is an explanation of exception handling in Java:
implementation of abstract methods must be provided by the subclasses that inherit from the abstract name of the package that the class belongs to. For example, the following
 Exception is an event that occurs during the execution of a class.
program that disrupts the normal flow of the program. Exceptions Here is an example of an abstract class in Java:
statement creates a package named com.example:
can be caused by a variety of factors, such as invalid input, errors abstract class Animal { package com.example;
in the program code, or hardware failures. abstract void speak(); } Importing a package
 Checked exceptions are exceptions that are checked by the This abstract class has one abstract method, speak(). This method must be implemented by any
subclass that inherits from Animal.
To import a package, you need to use the import statement at the top of
compiler at compile time. This means that the compiler will force
Polymorphism your Java source file. The import statement specifies the name of the
the programmer to handle the exception or declare it in the
Polymorphism is the ability of an object to take on different forms. In Java, polymorphism can be package that you want to import. For example, the following statement
throws clause of the method.
achieved through method overriding and method overloading.
 Unchecked exceptions are exceptions that are not checked by Method overriding occurs when a subclass provides a different implementation for a method that is
imports the com.example package:
the compiler at compile time. This means that the compiler will already defined in the superclass. For example, the following code shows a subclass of Animal that Code snippet
not force the programmer to handle the exception. overrides the speak() method: import com.example.*;
 The try statement is used to define a block of code that is to be class Dog extends Animal {
@Override
Example
tested for errors. If an error occurs in the try block, the control is
transferred to the catch block. void speak() { Here is an example of how to create a package and import it in another Java
System.out.println("Woof!"); } program:
 The catch statement is used to handle exceptions. The catch
}In this example, the speak() method in the Dog class overrides the speak() method in the Animal class.
statement must be preceded by a try block. The catch statement When a Dog object is created, the speak() method in the Dog class will be called.
// package declaration package com.example;
can catch a specific type of exception or a group of exceptions. Method overloading occurs when a class has multiple methods with the same name, but different // class declaration public class MyClass {
 The throw statement is used to throw an exception. The throw parameters. For example, the following code shows a class with two methods named add(): public static void main(String[] args) { // import statement
statement can throw a specific exception or a group of class Calculator {
int add(int a, int b) {
import com.example.OtherClass;
exceptions.
 The throws clause is used to declare exceptions that can be return a + b; } // create an object of OtherClass OtherClass otherClass = new OtherClass();
int add(int a, int b, int c) { // call a method of OtherClass
thrown by a method. The throws clause is used in the method
return a + b + c; }
declaration. }
otherClass.doSomething(); }
 The finally statement is used to execute code that must be In this example, the add() method is overloaded. The first add() method takes two parameters, while the }
executed regardless of whether an exception occurs. The finally second add() method takes three parameters. The method that will be called depends on the number there are four levels of access protection: Public: A public class or
statement is used after the try and catch blocks. and type of parameters that are passed to the method.
abstract class Shape
member can be accessed from anywhere.
Here are two examples of exceptions:
 ArithmeticException is thrown when an arithmetic operation {  Protected: A protected class or member can be accessed from within
abstract void draw(); its package and by
results in an invalid value, such as dividing by zero.
}
 FileNotFoundException is thrown when a file cannot be found.
class Circle extends Shape {
subclasses in other packages.
Protected: A protected class or member can be accessed from @Override void draw() {  Package-private: A package-private class or member can only be
within its package and by System.out.println("Drawing a circle..."); } accessed from within its package.
subclasses in other packages. }
 Package-private: A package-private class or member can only be class Rectangle extends Shape { @Override
 Private: A private class or member can only be accessed from within
accessed from within its package. void draw() { its own class.
 Private: A private class or member can only be accessed from System.out.println("Drawing a rectangle..."); x
}}
within its own class.
public class Main {
public static void main(String[] args) {
Shape shape = new Circle(); shape.draw();
shape = new Rectangle();
shape.draw(); }
}

You might also like