java tools
-------------------
javac
java
javap
appletviewer
javah
jdb
javadoc
// rect program
class Rect
{
int len,bre;
void get(int l,int h)
{
len=l;
bre=h;
}
void area(){
System.out.println("Area ="+len*bre)
}
public static void main(String a[])
{
Rect r=new Rect();
r.area();
r.get(10,11);
r.area();
}
}
-------------------------17-8-24
import java.lang.Math;
public class Main
{
public static double PI=3.14F;
public static void main(String[] args) {
System.out.println(Math.sqrt(4));
System.out.println(Math.ceil(4.3));
System.out.println(Math.floor(4.3));
System.out.println(PI);
}
}
2.0
5.0
4.0
3.140000104904175
--------------------------------------------------------
import java.lang.Math;
class A{
final void dis(){}
}
class B extends A
{
void dis(){}
}
output:
Main.java:10: error: dis() in B cannot override dis() in A
void dis(){}
^
overridden method is final
1 error
-----------------------------------------------------
import java.lang.Math;
final class A{
void dis(){}
}
class B extends A
{
void dis(){}
}
public class Main
{
public static void main(String[]a)
{
B bobj=new B();
bobj.dis();
}
}
output:
Main.java:8: error: cannot inherit from final A
class B extends A
^
1 error
java tools
-------------------
javac
java
javap
appletviewer
javah
jdb
javadoc
// rect program
class Rect
{
int len,bre;
void get(int l,int h)
{
len=l;
bre=h;
}
void area(){
System.out.println("Area ="+len*bre)
}
public static void main(String a[])
{
Rect r=new Rect();
r.area();
r.get(10,11);
r.area();
}
}
-------------------------17-8-24
import java.lang.Math;
public class Main
{
public static double PI=3.14F;
public static void main(String[] args) {
System.out.println(Math.sqrt(4));
System.out.println(Math.ceil(4.3));
System.out.println(Math.floor(4.3));
System.out.println(PI);
}
}
2.0
5.0
4.0
3.140000104904175
--------------------------------------------------------
import java.lang.Math;
class A{
final void dis(){}
}
class B extends A
{
void dis(){}
}
output:
Main.java:10: error: dis() in B cannot override dis() in A
void dis(){}
^
overridden method is final
1 error
-----------------------------------------------------
import java.lang.Math;
final class A{
void dis(){}
}
class B extends A
{
void dis(){}
}
public class Main
{
public static void main(String[]a)
{
B bobj=new B();
bobj.dis();
}
}
output:
Main.java:8: error: cannot inherit from final A
class B extends A
^
1 error
--------------inheritance-----------
import static java.lang.System.out;
class Student{
protected int sno;
protected String sname;
public Student() {
sno=10;
sname="xyz";
out.println("Student()");
}
public Student(int sn,String snm) {
sno=sn;
sname=snm;
out.println("Student(int,String)");
}
}
class Mpc extends Student{
private int maths,phy,che;
public Mpc() {
maths=phy=che=40;
out.println("Mpc()");
}
public Mpc(int sn,String snm,int m,int p,int c) {
super(sn,snm);
maths=m;phy=p;che=c;
out.println("Mpc(int,int,int)");
}
public void dis() {
out.println(sno+"\t"+sname);
out.println(maths+"\t"+phy+"\t"+che);
}
}
public class InheritanceTest {
public static void main(String[] args) {
Mpc m=new Mpc();
m.dis();
Mpc m1=new Mpc(101,"Raj",90,80,77);
m1.dis();
}
}
Output :
Student()
Mpc()
10 xyz
40 40 40
Student(int,String)
Mpc(int,int,int)
101 Raj
90 80 77
------------call by val --------------
class Swap{
int x,y;
public Swap(int a,int b){
x=a;
y=b;
}
public void swapval(){
int tmp;
tmp=x;x=y;y=tmp;
}
public void disval(){
System.out.println("x="+x+" y="+ y);
}
}
public class CallByVal
{
public static void main(String[] args) {
int L=10,M=20;
Swap s=new Swap(L,M);
s.disval();
s.swapval();
System.out.println(L+"\t\t"+M);
}
}
---------------------- menu program--------------------
import java.util.Scanner;
class Account{
int ano;
double amt;
public void addacc(int a,double amt){
ano=a;
this.amt=amt;
}
public void dis(){
System.out.println(ano+"\t\t"+amt);
}
public void credit(double cr){
amt+=cr;
}
}
public class Main
{
public static void main(String[] args) {
Account a=new Account();
String ch;
int cho;
Scanner in=new Scanner(System.in);
while(true){
System.out.println("1 add account");
System.out.println("2 dis account");
System.out.println("3 credit amount");
System.out.println("0 EXIT");
System.out.println("Choice?");
ch=in.nextLine();
cho=Integer.parseInt(ch);
switch(cho){
case 1:
a.addacc(1001,10000.5);
break;
case 2:
a.dis();
break;
case 3: a.credit(1000); break;
case 0: System.exit(0);
}
}
}
}
-------------------------------------------------------------