Java Basic Programs
Write your java program on a notepad screen. Save the program with .java extension.
The name of the program should be the name of the class containing the main function.
The How to run a java program through cmd??
Go to start menu button, search for cmd
In the command prompt screen direct to
the folder where your java programs reside… using cd.. command.
For example to reach F: Studies folder
cd..
F:
Cd Studies
So now once you reach there type the
following commands
Javac nameofprogram.java
Java nameofprogram
Solve the errors and run the program !!
//Java Program to demonstrate
Overloading
class overloading
{
public static void main(String[]
args)
{
functionOverload obj =
new functionOverload();
obj.add(1,2);
obj.add(11.5, 22.5);
obj.add("Palak
","Sanghani");
}
}
class functionOverload
{
void add(int a, int b, int c)
{
int sum = a + b + c;
System.out.println("Sum of
a+b+c is: " + sum);
}
void add(double a, double b)
{
double sum = a + b;
System.out.println("Sum of
a+b is: " + sum);
}
void add(String s1, String s2)
{
String s = s1+s2;
System.out.println(s);
}
}
//Java Program to accept user input
import java.util.Scanner;
public class userinput
{
public
static void main(String[] args)
{
Scanner
user_input = new Scanner(System.in);
String
FirstName;
System.out.println("Enter
First Name: ");
FirstName
= user_input.next();
String
LastName;
System.out.println("Enter
Last Name: ");
LastName
= user_input.next();
System.out.println("Welcome,
" + FirstName + " " + LastName);
}
}
Comments
Post a Comment