Java Hyd Team
746 subscribers
986 photos
39 videos
670 files
690 links
https://teamhydteam.my.canva.site/

Can visit us on our website 😊
Still working on it 😊
Download Telegram
Anyone wanna join this ?
1
public class Student { private int studentId; private String studentName; private int marks; private char grade; public Student() { // TODO Auto-generated constructor stub } public Student(int studentId, String studentName, int marks) { super(); this.studentId = studentId; this.studentName = studentName; this.marks = marks; calculateGrade(); } public String displayDetails() { return "Student [name=" + studentName + ", studentId=" + studentId + ", marks=" + marks + ", grade=" + grade + "]"; } private void calculateGrade() { if (marks <= 100 && marks >= 90) { grade = 'A'; } else if (marks <= 89 && marks >= 81) { grade = 'B'; } else if (marks <= 80 && marks >= 71) { grade = 'C'; } else if (marks <= 70 && marks >= 61) { grade = 'D'; } else if (marks <= 60 && marks >= 0) { grade = 'E'; } } }

public class Tester { public static void main(String[] args) { Student s1 = new Student(123, "John Smith", 95); System.out.println(s1.displayDetails()); } }

Output:

Student [name=John Smith, studentId=123, marks=95, grade=A]
There are five different ways to create an object in Java:
Java new Operator
Java Class.newInstance() method
Java newInstance() method of constructor
Java Object.clone() method
Java Object Serialization and Deserialization
class_name object_name = new class_name()
public T newInstance() throws IllegalAcccessException
public T newInstance(Objects...initargs)
public class Test {
public static void main(String[] args) {
String input = "sat23h6y7a8";
int sum = 0;
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isDigit(ch)) {
sum = sum + Integer.parseInt(String.valueOf(ch));
}
}
System.out.println(sum);
}
}
Difference between final finally and finalize

The main difference between final, finally, and finalize in Java is that final is a keyword, finally is a block, and finalize is a method.

A final variable can only be assigned once and it cannot be changed.
A finally block is used to execute important code such as closing a connection, stream, or file.
A finalize method is used to perform cleanup operations before an object is destroyed.
//Write a java program for Fibonacci Series from 1 to 100?

public class FibonacciExample1 {
public static void main(String[] args) {
int n1=0,n2=1,n3,i,count=100;
System.out.print(n1+" "+n2);//printing 0 and 1

for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}

}}
int nums []= x.nextInt();
int[] count = new int[101];
int[] res = new int[nums.length];

for (int i =0; i < nums.length; i++) {
count[nums[i]]++;
}

for (int i = 1 ; i <= 100; i++) {
count[i] += count[i-1];
}

for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0)
res[i] = 0;
else
res[i] = count[nums[i] - 1];
}

return res;
}
import java.io.*;
import java.util.Scanner;

public class NumerSmallerThanCurrentNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the size of array : ");
int n = scanner.nextInt();


//Declaring the array
int arr[] = new int[n];

System.out.println("Enter the array elements : ");

for(int i=0;i<n;i++) {
arr[i] = scanner.nextInt();
}

System.out.println("The Entered elements are : ");
for(int i=0;i<n;i++) {
System.out.print(arr[i] + " ");
}

int listOfElements[] = countSmallerNumber(arr);

System.out.println("\nThe list of numbers smaller than current number : ");

for(int i=0;i<listOfElements.length;i++) {

System.out.print(listOfElements[i]+" ");
}
}

//Function for count the smaller element than the current element
private static int[] countSmallerNumber(int[] arr) {

//Take a array for storing the the count of smaller element than the current element
int countNumber[] = new int[arr.length];

for(int i=0;i<arr.length;i++) {

int count=0;

for(int j=0;j<arr.length;j++) {

//Compare current elements with other elements
if((arr[i]>arr[j]) && (i!=j)) {

//Increase the count, if other element is smaller than the current element
count++;
}
}
countNumber[i]=count;
}

return countNumber;
}
}