PhysicsTeacher.in

High School Physics

Java_BlueJ_Programming_22

Chemical equation and balancing chemical equations

In this post, we will discuss chemical reactions, chemical equations & the balancing of chemical equations. Chemical Relation & chemical equation – definition & explanation A chemical reaction expresses a chemical change. For example, one chemical property of hydrogen is that it will react with oxygen to make water. We can write that as follows: […]

Java Program – how to use constructors in a Java class

Here is a Java program that helps you to understand the concepts of constructors in Java class. This program also tells us how to implement constructors correctly in Java programs to instantiate or create objects or instances. how to use constructors in a Java class | a Java program showing constructors class TestConstructor{int i,j; TestConstructor(){i=5;j=6;} […]

Java Program to test polymorphism

Here is a Java program that helps you to understand the concepts of polymorphism. This program also tells us how to implement polymorphism correctly in Java programs. Here, we have also shown a common mistake and discussed how to avoid it. polymorphism in a Java program – how to overload a method correctly? class TestPolyMorph{int […]

Java program showing features of String class

Here is a Java program that helps you to understand how to use some important features of the String class. Multiple ways of creating String instances (objects) are demonstrated in this program. String concatenation and String array are also shown in this java program. Java program showing different ways to instantiate String class | String […]

Java program with String and StringBuffer

Here is a Java program that helps you to understand how immutable String and growable StringBuffer classes differ in their behavior and operation. Java Program showing use of String and StringBuffer class StringB{public static void main(String args[]){//immutable String String s1 = “ABC”;String s2= s1;System.out.println(“s2=s1:::”+(s2==s1));System.out.println(“s2.equals(s1):::”+(s2.equals(s1))); s1=s1+”CDE”;System.out.println(“s2=s1:::”+(s2==s1));System.out.println(“s2.equals(s1):::”+(s2.equals(s1))); //Growable StringBufferStringBuffer sb1=new StringBuffer(“ABC”);StringBuffer sb2=sb1; System.out.println(“sb2=sb1:::”+(sb2==sb1));System.out.println(“sb2.equals(sb1):::”+(sb2.equals(sb1)));sb1.append(“CDE”); System.out.println(“sb2=sb1:::”+(sb2==sb1));System.out.println(“sb2.equals(sb1):::”+(sb2.equals(sb1)));}}

Java Program – how to use static variable and static method?

This Java Program will show how to use static variables and static methods in Java. Java Program showing use of static variables and static methods class StaticTest{ static int j;int k; void setVal(int p){k=p;j=p;//ok ..non-static method can access a static variable }static void setStatVal(int p){ j=p;//k=p; /*the above statement wont compile, because static can access […]

Java program – how to use the Scanner class to capture user input

Here is a Java program that helps you to understand how to use the Scanner class to capture user input. The Scanner class is used to get user input, and it is found in java.util package. Using Scanner class in Java program (code of the program) import java.util.Scanner;//import safetutorial.course.professional.java.*; public class ScannerTest //public main class, to be […]

Java Program – how to use DataInputStream

Here is a Java program that helps you to understand how to use DataInputStream to capture user input. Java Program – how to use DataInputStream to capture user input using readLine import java.io.DataInputStream;class Reading{ public static void main(String args[]){DataInputStream din= new DataInputStream(System.in);int i1=0;//initialization must to compile//error: variable i1 might not have been initializedfloat f1=0.0f;//initialization must […]

Java Program – overriding a method inherited from Object class

Here is a Java program that helps you to understand how to override a method. In this program, a class overrides the “equals” method inherited from the Object class in Java. Java program to Override a method inherited from Object class in Java class Room{int l,b,h;Room(int l,int b,int h){this.l=l;this.b=b;this.h=h;} void show(){System.out.println(“DATA:”+l+” “+b+” “+h);} /* overriding […]

Java program – using BufferedReader & InputStreamReader

Here is a Java program that helps you to understand the use of BufferedReader & InputStreamReader. Type, compile, and run to understand how to use the BufferedReader & InputStreamReader in a Java program. In this program, multiple lines of text can be received as input until a specific word is not entered as line content. […]

Java program – how to use “this” keyword?

Here is a Java program that helps you to understand the concepts of “this” keyword in Java. Type, compile and run to understand how to use the concepts and features of “this” in a Java program. Also, note the overall structure of a set of java classes. (4 Java classes are used in this single […]

Java program – how to access the input arguments

Here is a Java program that helps you to access the input arguments. If you have noticed this statement: public static void main (String args[]) in a java program, and want to know the use of that String args[], then you are at the right place. Type, compile, and run to understand how to use […]

Java program – how to access private variable

This Java program shows how to access a private variable. This will help you to understand the use of access specifiers. Access specifier in Java program | Accessing private variable using Java class AccessTest{int i; //default accesspublic int j;private int k; /*accessing private variable through a method*/ void setVal (int p){k=p;} void writeVal(){System.out.println(“value of i=”+i);System.out.println(“value […]

Java Program with Abstract classes

Here is a Java program that helps you to understand the concepts of Abstract classes. Type, compile and run to understand how to use the concepts of Abstract class in a Java program. Java program to understand the concepts of Abstract class //a class can be declared abstract even if it does not have any […]

simple Java program using constructor

Here you will find a simple Java program that uses a constructor method in a class. Also the use of “this” is shown in the example. The use of new is also demonstrated to instantiate new instances of a class. Java program – using constructor, “new” and “this” package test;class A{int i; A(int i){ this.i=i; […]

Write a Java program to display the first 10 numbers of the Fibonacci

How to write a Java program to display the first 10 numbers of the Fibonacci series. Fibonacci number with Java Program public class Fibonacci{public static void main(String[] args) {int num1=0, num2=1;int i;int fibo;System.out.println(“PRINTING FIRST 10 FIBONACCI NUMBERS”);System.out.print(” “+num1); System.out.print(” “+num2);for(i=3;i<=10;i++){fibo=num1 + num2;num1=num2;num2=fibo; System.out.print(” “+fibo);}}}

Java Program to find the greatest and smallest numbers

Write a Java program to accept 10 different numbers and display the greatest and smallest numbers from the set of numbers entered by the user. Find the greatest and smallest numbers – Java Program import java.util.Scanner;public class HighestLowest {public static void main(String[] args) {int num,i; int highest=0,lowest=0;Scanner s = new Scanner(System.in);System.out.println(“Enter 10 different numbers:”);for(i=1;i<=10;i++) { […]

Java Program for prime number checking

Let’s see how to write a Java program to accept a number and check whether the number is a Prime number or not. A prime number is a whole number greater than 1 whose only factors are 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, […]

Java program to find HCF (highest common factor) of 2 numbers

Let’s see how to write a Java program to find the HCF (highest common factor) of 2 numbers. Here is a Java program that accepts 2 numbers and finds the greatest common divisor or HCF (highest common factor) of those numbers. Java Program to find HCF import java.util.Scanner;public class HCF { public static void main(String […]

Write a Java program to find the factorial of a given number (using loop)

Let’s see how to write a Java program to find the factorial of a given number (using loop). Java program to find factorial class Factorial{ public static void main(String args[]){int i; long fact=1;int number=10;//It is the number whose factorial is calculated, modify it if needed for(i=1;i<=number;i++){fact=fact*i;System.out.println(“printing from inside loop:”+fact);} System.out.println(“Factorial of “+number+” is: “+fact);}}

Write a Java program to find the area, perimeter, and diagonal of a rectangle.

Here is a Java program that finds the area, perimeter, and diagonal of a rectangle. Also, this program finds the volume of a cube. Java program – code import java.util.Scanner;public class RectangleCubeMeasurement{ public static void main(String args[]){float length,breadth,area,perimeter;double diagonal;float side; double volume;Scanner sc = new Scanner(System.in);System.out.println(“Calculation of Area, perimeter and diagonal of a rectangle”);System.out.println(“Enter the […]

Write a Java program to check whether a number is even or odd

Let’s see how to write a Java program to check whether a given number is even or odd by using if-else. Java program to determine even or odd number import java.util.Scanner;public class Evenodd {public static void main(String args[]){int a;Scanner sc = new Scanner(System.in);System.out.println(“Enter the number to be tested :: “); a = sc.nextInt();if( a%2 == […]

Automorphic Number Java Program – check if a Number is an Automorphic

This post presents a Java Program to check if a given Number is an Automorphic number. Automorphic number Java Program – code import java.util.Scanner;public class Automorphic {public static void main(String args[]){Scanner sc = new Scanner(System.in);System.out.print(“Input a number : “);int num = sc.nextInt(); int sq_num = num*num;String str_num = Integer.toString(num);String square = Integer.toString(sq_num); if(square.endsWith(str_num))System.out.println(“Automorphic Number.”);elseSystem.out.println(“Not an Automorphic […]

Perfect Number Java Program – check if a Number is a Perfect Number

This post presents a Java Program to check if a given Number is a Perfect Number. A perfect number is a positive integer that is equal to the sum of its proper positive divisors excluding the number itself. Perfect Number Java Program – code import java.util.Scanner;public class Perfect{public static void main(String[] args){ int n, sum […]

Pattern output using Java program

Here is a set of Java programs that are written to generate pattern output (like alphabet pattern). These programs are useful for students who are learning Java (or BlueJ) programming. Students of ICSE, CBSE, and state boards of India will find it useful to prepare their school projects as well as board projects. Alphabet pattern […]

Palindrome Program in Java using String methods

Here is a Java program that is written as a Palindrome Program in Java using String methods. This program allows the user to enter a string/number. Then the program checks whether the input is a Palindrome or not, and then responds back accordingly. This program is useful for students who are learning Java (or BlueJ). […]

Scroll to top
error: physicsTeacher.in