Java Imp
Java Imp
a.) sqrt()
b.) pow()
All the Math functions in Java lie under the Package of java.lang.Math
sqrt():- sqrt() is a Math Function in Java, which is used for finding out the Square- Root of a specific Number. The
Short Form of Square Root, i.e. sqrt is used here.
SYNTAX:- Math.sqrt(a)
pow():- pow() is used to calculate a number raise to the power of some other number. This function accepts two
parameters and returns the value of first parameter raised to the second parameter.
SYNTAX:- public static double pow(double a, double b)
Give any two Methods from File Class with their useage in Java
Method Description Return Type
canRead() Tests whether the application can read the file denoted by this abstract pathname. boolean
canWrite() Tests whether the application can modify the file denoted by this abstract pathname. boolean
createNewFile() Atomically creates a new, empty file named by this abstract pathname. boolean
delete() Deletes the file or directory denoted by this abstract pathname. boolean
getAbsolutePath() Returns the absolute pathname string of this abstract pathname. String
getFreeSpace() Returns the number of unallocated bytes in the partition. long
getName() Returns the name of the file or directory denoted by this abstract pathname. String
getParent() Returns the pathname string of this abstract pathname’s parent. String
getParentFile() Returns the abstract pathname of this abstract pathname’s parent. File
getPath() Converts this abstract pathname into a pathname string. String
setReadOnly() Marks the file or directory named so that only read operations are allowed. boolean
isHidden() Tests whether the file named by this abstract pathname is a hidden file. boolean
length() Returns the length of the file denoted by this abstract pathname. long
toString() Returns the pathname string of this abstract pathname. String
toURI() Constructs a file URI that represents this abstract pathname. URI
Write a program to find largest between two numbers using "?:" Operator
public class Main
{
public static void main (String[]args)
{
int num1 = 50, num2 = 10, temp;
if (num1 == num2)
System.out.println ("Both are Equal\n");
else
{
temp = num1 > num2 ? num1 : num2;
System.out.println (temp + " is largest");
}
}
}
Define a Class “Student” with suitable Data- Members. Create two Objects using two different Constructors
of the Class.
import java.util.Scanner;
public class Student{
private String name;
private int age;
private int m1;
private int m2;
private int m3;
private int maximum;
private double average;
public Student(String n, int a, int s1, int s2, int s3) {
name = n;
age = a;
m1 = s1;
m2 = s2;
m3 = s3;
}
public Student() {
name = "";
age = 0;
m1 = 0;
m2 = 0;
m3 = 0;
maximum = 0;
average = 0;
}
public void accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
name = in.nextLine();
System.out.print("Enter age: ");
age = in.nextInt();
System.out.print("Enter Subject 1 Marks: ");
m1 = in.nextInt();
System.out.print("Enter Subject 2 Marks: ");
m2 = in.nextInt();
System.out.print("Enter Subject 3 Marks: ");
m3 = in.nextInt();
}
public void compute(){
if (m1 > m2 && m1 > m3)
maximum = m1;
else if (m2 > m1 && m2 > m3)
maximum = m2;
else
maximum = m3;
average = (m1 + m2 + m3) / 3.0;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Subject 1 Marks: " + m1);
System.out.println("Subject 2 Marks: " + m2);
System.out.println("Subject 3 Marks: " + m3);
System.out.println("Maximum Marks: " + maximum);
System.out.println("Average Marks: " + average);
}
public static void main(String args[]) {
Student obj = new Student();
obj.accept();
obj.compute();
obj.display();
}
}
Describe the Life- Cycle of a Thread with suitable Diagram.
1. New Thread: When a new thread is created, it is in the new state. The thread has not yet started to run when
the thread is in this state. When a thread lies in the new state, its code is yet to be run and hasn’t started to
execute.
2. Runnable State: A thread that is ready to run is moved to a runnable state. In this state, a thread might
actually be running or it might be ready to run at any instant of time. It is the responsibility of the thread
scheduler to give the thread, time to run.
A multi-threaded program allocates a fixed amount of time to each individual thread. Each and every thread
runs for a short while and then pauses and relinquishes the CPU to another thread so that other threads can get
a chance to run. When this happens, all such threads that are ready to run, waiting for the CPU and the
currently running thread lie in a runnable state.
3. Blocked/Waiting state: When a thread is temporarily inactive, then it’s in one of the following states:
Blocked
Waiting
4. Dead State: A thread terminates because of either of the following reasons:
Because it exits normally. This happens when the code of the thread has been entirely executed by the program.
Because there occurred some unusual erroneous event, like segmentation fault or an unhandled exception.
Enlist the types of Stream Classes and describe the Methods for Reading and Writing Data for each type.
Based on the data they handle there are two types of streams −
Byte Streams − These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits.
Using these you can store characters, videos, audios, images etc. InputStream is used for Reading and
OutputStream is used for Writing the Data.
Character Streams − These handle data in 16 bit Unicode. Using these you can read and write text data
only. Reader is used for Reading, and Writer is used for Writing the Data.
Write a Program to initialize the Object of a Class “Student” using Parameterized Constructor.
class Student4
{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n)
{
id = i;
name = n;
}
//method to display the values
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}
Write a Program to create package Math_s having two classes as Addition and Subtraction. Use suitable
Methods in each Class to perform basic Operations.
package Math_s;
public class addition
{
Int a,b;
public add(int x,int y)
{
a=x;
b=y;
return(a+b);
}
}
public class subtraction
{
Int a,b;
public sub(int x, int y)
{
a=x;
b=y;
return(a-b);
}
}
import Math_s.addition;
import Math_s.subtraction;
class Basic
{
public static void main(String args[])
{
addition a=new addition( );
int sum=a.add(10,5);
subtraction s = new subtraction();
int subt=s.sub(10,5);
System.out.println(“Addition= “+sum);
System.out.println(“Subtraction= “+subt);
}
}