java[1]
java[1]
Basic Programs
1.1 Study of class path and java runtime environment
There are two ways of set a path variable
1) By ordinary method
Open a Environment Variables Dialog box by search box
Click on New Button -> Enter PATH as a variable name and copy the path of bin file of JDK and paste to the
variable value -> click on OK.
2) By Command Prompt
Syntax: path [[<drive>:]<path>[;...][;%PATH%]]
import java.util.Scanner;
n1,n2,ans; int c;
System.out.println("Enter two
numbers:"); n1=sc.nextDouble();
n2=sc.nextDouble();
c=sc.nextInt();
2 : ans = n1 - n2;
4 : ans = n1 / n2;
System.out.println("Invalid case");
return ; } }
}
Output:
Output:
Practical 2
class Matrix{
int row,column; float mat[][] =
{{1,2},{3,4}}; public Matrix(int a[]
[]){} public Matrix(){} public
Matrix(int row,int column){}
public void readMatrix()
{ System.out.println("Matrix: ");
for (int i=0;i<2;i++){
for (int j=0;j<2;j++)
System.out.print((int)mat[i][j]+" ");
System.out.println(" ");
}
}
public void transpose()
{
float [][]transpose = new float[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
transpose[i][j]= mat[j][i];
}
}
System.out.println("Transpose:");
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
System.out.print((int)transpose[i][j]+" ");
}
System.out.println();//new line
}
}
public void multiplication(float[][] mat2){ float[]
[] c = new float[2][2];
System.out.println("Multiplication:");
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
Output:
Double D = Double.valueOf("12.0");
System.out.println("Double value: " +D);
Boolean B = Boolean.valueOf("true");
System.out.println("Boolean value: " +B); } }
Output:
Output:
Code:
import java.util.Scanner;
class Cipher1{ private
String plaintxt; private
String ciphertxt;
class Pr2_5{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Text: ");
String data = scanner.nextLine();
Cipher1 cipher = new Cipher1(data);
Output:
3.1Create a class BankAccount that has Depositor name , Acc_no, Acc_type, Balance
as Data Members and void createAcc() . void Deposit(), void withdraw() and void
BalanceInquiry as Member Function. When a new Account is created assign next
serial no as account number. Account number starts from 1
Code:
package com.mycompany.pr3_1;
class BankAccount<acc_no> {
String depositor_name;
static int acc_no = 0; String
acc_type; private int
balance;
void balanceInquiry(){
System.out.println(balance);
}
}
public class Pr3_1 { public static void
main(String[] args) { BankAccount one =
new BankAccount();
BankAccount two = new BankAccount();
two.createAccount("Shushit","Business");
one.deposit(450000);
one.withdraw(60000);
one.balanceInquiry();
Output:
3.2Create a class time that has hour, minute and second as data members. Create a
parameterized constructor to initialize Time Objects. Create a member Function
Time Sum (Time, Time) to sum two time objects. Code:
import java.util.Calendar;
public class CalendarExample {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println("Current Date and Time: " + calendar.getTime());
calendar.add(Calendar.MINUTE, 5);
System.out.println("5 minutes added to time: " + calendar.getTime()); }
}
3.3Define a class with the Name, Basic salary and dearness allowance as data
members.Calculate and print the Name, Basic salary(yearly), dearness allowance
and tax deduced at source(TDS) and net salary, where TDS is charged on gross
salary which is basic salary + dearness allowance and TDS rate is as per following
table.
Gross Salary TDS
Rs. 100000 and below NIL
Above Rs. 100000 10% on excess over
100000
DA is 74% of Basic Salary for all. Use appropriate member function
Code:
package com.mycompany.pr3_3;
}
}
Output: