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;
}