Java 2022 Solution || BIM STUDY Notes
// Q.N0 11) TO FIND THE SECOND HIGHEST VALUE IN AN ARRAY
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package bimnotessolutions; public class Regular2022 { public static void main(String[] args) { int a[] = {1,2,3,4,5}; for(int i=0;i<a.length;i++){ for(int j = i+1;j<a.length;j++){ if(a[i]>a[j]){ int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } int index = a.length-2; System.out.println("the second largest value is :"+a[index]); } } |
// Q.N0 12)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package bimnotessolutions; class Product{ private String name; private int qty,price; public Product(String name, int qty, int price) { this.name = name; this.qty = qty; this.price = price; } public String getName() { return name; } public int getQty() { return qty; } public int getPrice() { return price; } public int getTotal(){ return this.getQty()*this.getPrice(); } } public class Regular2022 { public static void main(String[] args) { Product p1 = new Product("bread",2,500); Product p2 = new Product("muffins",4,600); System.out.println("the total sum is :"+(p1.getTotal()+p2.getTotal())); } } |
// Q.N0 13) implementing thread using Runnable class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package bimnotessolutions; class ThreadExample implements Runnable{ public void run(){ try{ for(int i = 100 ; i <=200 ; i++){ Thread.sleep(1500); if(i%2==0){ System.out.print(i+" "); } } }catch(Exception err){ System.out.println(err); } } } public class Regular2022 { public static void main(String[] args) { ThreadExample te = new ThreadExample(); Thread t1 = new Thread(te); t1.start(); } } |
Q.NO 15)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package bimnotessolutions; class MyException extends Exception{ MyException(String msg){ super(msg); } } class Person{ private String name; private int age; public void setName(String name){ this.name = name; } public void setAge(int age) throws MyException{ if(age<0 || age>100){ throw new MyException("age should be between the age"); } else{ this.age =age; } } } public class Regular2022 { public static void main(String[] args) throws MyException { Person p = new Person(); p.setName("Ram"); try{ p.setAge(150); }catch(MyException err){ System.out.println(err); } } |
Continue Reading