Java methods
class Methods
{
Methods()
{
System.out.println("Constructor method is called when an object of it's class is created");
}
public static void main(String[] args)
{
staticMethod();
Methods object = new Methods();
object.nonStaticMethod();
}
static void staticMethod()
{
System.out.println("Static method can be called without creating object");
}
void nonStaticMethod()
{
System.out.println("Non static method must be called by creating an object");
}
}
Java String methods
class StringMethods
{
public static void main(String args[])
{
int n;
String s = "Java programming", t = "", u = "";
System.out.println(s);
n = s.length();
System.out.println("Number of characters = " + n);
t = s.replace("Java", "C++");
System.out.println(s);
System.out.println(t);
u = s.concat(" is fun");
System.out.println(s);
System.out.println(u);
}
}
Java program to convert
Fahrenheit to Celsius
import java.util.*;
class FahrenheitToCelsius
{
public static void main(String[] args)
{
float temperatue;
Scanner in = new Scanner(System.in);
System.out.println("Enter temperatue in Fahrenheit");
temperatue = in.nextInt();
temperatue = ((temperatue - 32)*5)/9;
System.out.println("Temperatue in Celsius = " + temperatue);
}
}
Java program to find odd
or even
import java.util.Scanner;
class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer to check if it is odd or even ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if ( x % 2 == 0 )
System.out.println("You entered an even number.");
else
System.out.println("You entered an odd number.");
}
}
Java program to add two
numbers
import java.util.Scanner;
import java.math.BigInteger;
class AddingLargeNumbers
{
public static void main(String[] args)
{
String number1, number2;
Scanner in = new Scanner(System.in);
System.out.println("Enter first large number");
number1 = in.nextLine();
System.out.println("Enter second large number");
number2 = in.nextLine();
BigInteger first = new BigInteger(number1);
BigInteger second = new BigInteger(number2);
BigInteger sum;
sum = first.add(second);
System.out.println("Result of addition = " + sum);
}
}
How to get input from
user in java
import java.util.Scanner;
class GetInputFromUser
{
public static void main(String args[])
{
int a;
float b;
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
s = in.nextLine();
System.out.println("You entered string "+s);
System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("You entered integer "+a);
System.out.println("Enter a float");
b = in.nextFloat();
System.out.println("You entered float "+b);
}
}
Java program to print
multiplication table
import java.util.Scanner;
class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
System.out.println("Enter an integer to print it's multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");
for ( c = 1 ; c <= 10 ; c++ )
System.out.println(n+"*"+c+" = "+(n*c));
}
}
Java program to print
alphabets
class Alphabets
{
public static void main(String args[])
{
char ch;
for( ch = 'a' ; ch <= 'z' ; ch++ )
System.out.println(ch);
}
}
Java while loop
import java.util.Scanner;
class WhileLoop
{
public static void main(String[] args)
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Input an integer");
while ((n = input.nextInt()) != 0)
{
System.out.println("You entered " + n);
System.out.println("Input an integer");
}
System.out.println("Out of loop");
}
}
Java while loop break program
import java.util.Scanner;
class BreakWhileLoop
{
public static void main(String[] args)
{
int n;
Scanner input = new Scanner(System.in);
while (true)
{
System.out.println("Input an integer");
n = input.nextInt();
if (n == 0)
{
break;
}
System.out.println("You entered " + n);
}
}
}
Java while loop break continue program
import java.util.Scanner;
class BreakContinueWhileLoop
{
public static void main(String[] args)
{
int n;
Scanner input = new Scanner(System.in);
while (true)
{
System.out.println("Input an integer");
n = input.nextInt();
if (n != 0)
{
System.out.println("You entered " + n);
continue;
}
else
{
break;
}
}
}
}
Java for loop
class Stars
{
public static void main(String[] args)
{
int row, numberOfStars;
for (row = 1; row <= 10; row++)
{
for(numberOfStars = 1; numberOfStars <= row; numberOfStars++)
{
System.out.print("*");
}
System.out.println();
}
}
}
No comments:
Post a Comment