Assignment Java
Assignment Java
Program:-
public class q3 {
public static void main(String[] args) {
int num = 29;
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = true;
break;
}
}
if (!flag) {
System.out.println(num + " is a prime number");
} else {
System.out.println(num + " is not prime number");
}
}
}
Output:-
import java.util.Scanner;
public class q4 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("enter a number");
int n = sc.nextInt();
int sum = 0;
int r;
int temp = n;
while (n != 0) {
r = n % 10;
sum = sum * 10 + r;
n = n / 10;
}
if (sum == temp) {
System.out.println("palindrome");
} else {
System.out.println("not palindrome");
}
}
}
Output:-
Que.5 write a program to check if a check is Armstrong number
or not ?
Program:-
import java.util.Scanner;
class Q5
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int n=sc.nextInt();
int sum=0;
int r;
int temp=n;
while(n!=0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(sum==temp)
{
System.out.println("amstrom");
}
else {
System.out.println("non amstrom");
} }}
}
}
Output:-
Q10. Find the length of the string without using length method?
Program:-
class A2q10
{
public static void main(String args[])throws Exception
{
String str="samplestring";
int i=0;
for(char c:str.toCharArray())
{
i++;
}
System.out.println("Length of the given string:"+i);
}}
Output :-
Q6. How to calculate total no of vowels in a string?
Program:-
class A2q6
{
public static void main(String args[])
{
String str="hey this is kamakshi";
int vovel=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='a' ||str.charAt(i)=='e' ||str.charAt(i)=='i' ||
str.charAt(i)=='o'|| str.charAt(i)=='u')
{
vovel++;
}}
System.out.println("No.of vovels="+vovel);
}}
Output:-
Q8. How does substring() method works in java?
class A2q8
{
public static void main(String args[])
{
String str="COMPUTER";
String p=str.substring(3);
System.out.println("Substring starting from index 3: " +p);
}}
Output:-
Program:-
import java.util.Scanner;
interface payable {
void getAmount(int i);
}
Programe :-
import java.util.Scanner;
interface vechicle
{
void getcolor(String h);
void getname(String n);
void getConsumption();
} class TwoWheelear implements vechicle
{ public
void getcolor(String h)
{
System.out.println("Colour of Bike is: " + h);
} public
void getname(String n)
{
System.out.println("Name of Bike is: " + n);
} public
void getConsumption()
{
float liters, km;
Scanner o = new Scanner(System.in);
System.out.print("Enter Kilometer Driven By Driver: ");
km = o.nextFloat();
float fuel = (km/50) ;
System.out.println("Your Fuel Consumption is: " + fuel + " liters");
}
} class FourWheelear implements vechicle
{ public
void getcolor(String h)
{
System.out.println("Colour of Car is: " + h);
} public
void getname(String n) { System.out.println("Name of Car is: " + n); }
public
void getConsumption()
{
float liters, km;
Scanner o = new Scanner(System.in);
System.out.print("Enter Kilometer Driven By Driver: ");
km = o.nextFloat();
float fuel = (km/10);
System.out.println("Your Fuel Consumption is: " + fuel + "
liters");
}
}
public class aq3_4
{
static void main(String[] args)
{ int k;
System.out.print("Enter 1 for Two Wheeler and 2 for Four Wheeler:
");
Scanner p = new Scanner(System.in);
k = p.nextInt();
switch (k)
{ case 1:
TwoWheelear l = new TwoWheelear();
l.getcolor("Black");
l.getcolor("CBR 600 RR");
l.getConsumption();
break;
case 2:
FourWheelear i = new FourWheelear();
i.getcolor("Neon blue");
i.getname("Lamborghini");
i.getConsumption();
break;
}}}
Output :-
Enter 1 for Two Wheeler and 2 for Four Wheeler: 1
Colour of Bike is: Black
Colour of Bike is: CBR 600 RR
Enter Kilometer Driven By Driver: 6
Your Fuel Consumption is: 0.12 liters
Output:2
Enter 1 for Two Wheeler and 2 for Four Wheeler: 2
Colour of Car is: Neon blue
Name of Car is: Lamborghini
Enter Kilometer Driven By Driver: 6
Your Fuel Consumption is: 0.6 liters
interface fare {
void getAmount();
}
Output:-
import java.util.Scanner;
interface studentfee {
void getAmount();
void getFirstName();
void getLastName();
void getAddress();
void getContact();
}
class hostler implements studentfee {
Scanner i = new Scanner(System.in);
public void getFirstName() {
String name;
System.out.print("Enter Student First Name: ");
name = i.nextLine();
}
public void getLastName() {
String last;
System.out.print("Enter Student Last Name: ");
last = i.nextLine();
}
public void getAddress() {
String address;
System.out.print("Enter Student Full Address : ");
address = i.nextLine();
}
public void getContact() {
long num;
System.out.print("Enter Student Phone Number: ");
num = i.nextLong();
}
public void getAmount() {
int fee = 75000;
System.out.println("Annual Fees for Hostler Student is: " + fee);
}
}
class nonhostler implements studentfee {
Scanner i = new Scanner(System.in);
public void getFirstName() {
String name;
System.out.print("Enter Student First Name: ");
name = i.nextLine();
}
public void getLastName() {
String last;
System.out.print("Enter Student Last Name: ");
last = i.nextLine();
}
public void getAddress() {
String address;
System.out.print("Enter Student Full Address : ");
address = i.nextLine();
}
public void getContact() {
long num;
System.out.print("Enter Student Number: ");
num = i.nextLong();
}
public void getAmount() {
int fee = 88000;
System.out.println("Annual Fees for Non-Hostler Student is: " +
fee);
}
}
Output:2
Enter 1 For Hostler Student And 2 for Non-Hostler: 2
>>Non-Hostler Student<<
Enter Student First Name: Rohan
Enter Student Last Name: Sharma
Enter Student Full Address : M.p Nagar
Enter Student Number: 9875624159
Annual Fees for Non-Hostler Student is: 88000
package nikesh;
import java.util.Scanner;
class hrc {
void sum() {
int g, r, t;
Scanner i = new Scanner(System.in);
System.out.print("Enter First Number: ");
r = i.nextInt();
System.out.print("Enter Second Number: ");
t = i.nextInt();
g = r + t;
System.out.println("Sum of " + r + " and " + t + " is: " + g);
}
}
class hrc2 {
void multiply() {
float g, r, t;
Scanner i = new Scanner(System.in);
System.out.print("Enter First Number: ");
r = i.nextFloat();
System.out.print("Enter Second Number: ");
t = i.nextFloat();
g = r * t;
System.out.println("Multiplication of " + r + " and " + t + " is: " +
g);
}
}
Program 2:-
import java.util.Scanner;
import nikesh.hrc;
import nikesh.hrc2;
case 2:
hrc2 p = new hrc2();
p.multiply();
break;
}}}
Output:-
Output : 1
Choose 1 for sumand Choose 2 for Multiplication: 1
Enter First Number: 89
Enter Second Number: 56
Sum of 89 and 56 is: 145
Output : 2
Choose 1 for sumand Choose 2 for Multiplication: 2
Enter First Number: 56
Enter Second Number: 48
Multiplication of 56.0 and 48.0 is: 2688.0
Assignment 4
import java.io.IOException;
import java.util.Scanner;
Output:-
Output: 1
Output: 2
import java.util.Scanner;
public class array {
public static void main(String[] args) {
try {
Scanner o = new Scanner(System.in);
int n;
System.out.print("Enter Number Of elements you want to
store: ");
n = o.nextInt();
int arr[] = new int[n];
System.out.print("Enter Elements in Array: ");
for (int i = 0; i < n; i++) {
arr[i] = o.nextInt();
}
System.out.print("You Entered Elements in Array are: ");
for (int e = 0; e < n; e++) {
System.out.print(arr[e]);
}
} catch (ArrayIndexOutOfBoundsException r) {
System.out.println("Exception is: " + r);
}}}
Output:-
import java.util.Scanner;
class solve {
void question() {
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of a: ");
double a = input.nextDouble();
System.out.print("Enter the value of b: ");
double b = input.nextDouble();
System.out.print("Enter the value of c: ");
double c = input.nextDouble();
double d = b * b - 4.0 * a * c;
if (d > 0.0) {
double r1 = (-b + Math.pow(d, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(d, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
} else if (d == 0.0) {
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
} else {
System.out.println("Roots are not real.");
}}}
Output:-
Output:1
Enter the value of a: 5
Enter the value of b: 56
Enter the value of c: 5
The roots are -0.09000907441763602 and -11.109990925582363
Output:2
Enter the value of a: 5
Enter the value of b: 6
Enter the value of c: 4 Roots are not real.
Output:-
Program:-
import java.lang.*;
public class ThreadPriorityExample extends Thread
{
public void run()
{
System.out.println("Inside the run() method");
}
public static void main(String argvs[])
{
ThreadPriorityExample th1 = new ThreadPriorityExample();
ThreadPriorityExample th2 = new ThreadPriorityExample();
ThreadPriorityExample th3 = new ThreadPriorityExample();
System.out.println("Priority of the thread th1 is : " +
th1.getPriority());
System.out.println("Priority of the thread th2 is : " +
th2.getPriority());
System.out.println("Priority of the thread th2 is : " +
th2.getPriority());
th1.setPriority(6);
th2.setPriority(3);
th3.setPriority(9);
System.out.println("Priority of the thread th1 is : " +
th1.getPriority());
System.out.println("Priority of the thread th2 is : " +
th2.getPriority());
System.out.println("Priority of the thread th3 is : " +
th3.getPriority());
System.out.println("Currently Executing The Thread : " +
Thread.currentThread().getName());
System.out.println("Priority of the main thread is : " +
Thread.currentThread().getPriority());
Thread.currentThread().setPriority(10);
System.out.println("Priority of the main thread is : " +
Thread.currentThread().getPriority());
}}
Output:-
Program:-
import java.util.LinkedList;
public class AQ5_2 {
public static void main(String[] args)
throws InterruptedException
{
final PC pc = new PC();
t1.start();
t2.start();
// t1 finishes before t2
t1.join();
t2.join();
}
import java.util.concurrent.Semaphore;
class Que {
int item;
static Semaphore Con = new Semaphore(0);
static Semaphore Prod = new Semaphore(1);
void get()
{
try {
Con.acquire();
}
catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Consumer consumed item: " + item);
Prod.release();
}
void put(int item)
{
try {
Prod.acquire();
}
catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.item = item;
System.out.println("Producer produced item: " + item);
Con.release();
}
}
class Producer implements Runnable {
Que q;
Producer(Que q)
{
this.q = q;
new Thread(this, "Producer").start();
}
Output:-
Q4. Create an application of cash withdrawal from the bank
account that have no of users that are operating the accounts
(synchronization).
Program:-
class Bank {
int total = 100;
void withdrawn(String name, int withdrawal)
{
if (total >= withdrawal) {
System.out.println(name + " withdrawn " + withdrawal);
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
void deposit(String name, int deposit)
{
System.out.println(name + " deposited " + deposit);
total = total + deposit;
System.out.println("Balance after deposit: " + total);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class AQ5_4 {
public static void main(String[] args)
{
Bank obj = new Bank();
obj.withdrawn("Arnab", 20);
obj.withdrawn("Monodwip", 40);
obj.deposit("Mukta", 35);
obj.withdrawn("Rinkel", 80);
obj.withdrawn("Shubham", 40);
}
}
Output:-
Q5. Create a class called mycurrent that implements runnable
interface.
1) The mycurrentDate class displays the current date time 10
times ,with 100 millisecond interval – use sleep() method for
interval.
Program:-
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class AQ5_5 {
public static void main(String[] args) {
System.out.println("The required packages have been
imported");
System.out.println("A LocalDateTime object has been
defined");
DateTimeFormatter date_time =
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println("The current date and time is: "
+date_time.format(now));
}
}
Output:-
Program:-
Output:-
Assignment 6
Q1