Saturday, October 1, 2016
Wednesday, September 28, 2016
Java- Constructors
This program implements constructors in Java( parametric and non-parametric). Comment below. Have fun!!
class cons
{
int x;
cons()
{
this(1);
System.out.println("1");
}
cons(int s)
{
this('d');
System.out.println("2");
}
cons(char c)
{
this(5.6);
System.out.println("3");
}
cons(double f)
{
this(true);
System.out.println("4");
}
cons(boolean b)
{
System.out.println("5");
}
public static void main(String args[])
{
cons c1=new cons();
}
}Java- Constructor Chaining
This program implements constructor chaining. Comment below. Have fun!!!
//cannot use this and super in same constructor
class X
{
public X(int i)
{
}
}
class Y extends X
{
public Y(int i)
{
super(i);
}
}
class Z extends Y
{
public Z(int i)
{
super(10);
}
}
class ChainDemo
{
public static void main(String[] args)
{
Z ob = new Z(10);
}
}
//finalise with object to force garbage collectionJava- Smallest and Greatest Element of Array
This programs implements array and finds the smallest and greatest element of array. Do comment below. Have fun!!
class Arr
{
Arr()
{
}
int[] Ret(int a[])
{
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length-1;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
return a;
}
public static void main(String ... args)
{
Arr a = new Arr();
int b[]={9,1,3,0,2,4};
int c[]=a.Ret(b);
System.out.println("Smallest is"+c[0]);
System.out.println("Largest is"+c[c.length - 1]);
}
}
Java- Arrays
This program shows demo on how to implement arrays in Java. Have Fun!!
class Customer
{
int id;
String name;
public Customer()
{
id=0;
name="";
}
public Customer(int i,String n)
{
id=i;
name=n;
}
}
class ArraysDemo
{
public static void main(String[] args)
{
Customer[] c= new Customer[2]; //object array
c[0]=new Customer(); //need to initialize
c[1]=new Customer();
System.out.println(c[0].name);
int[] a=new int[10];
String[] str = new String[5];
for(String s:str)
{
System.out.println(s);
} //for each loop on string type
for(int v:a)
{
System.out.println(v);
} //for each loop
for(int i=0;i<a.length;i++)
{
a[i]= new java.util.Scanner(System.in).nextInt();
}
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
/*System.out.println(i);*/ //error due to scope
System.out.println(a);
}
}Sunday, September 25, 2016
Java-Abstract Class
//Abstract Class
abstract class AbstractComponent
{
//abstract method can be in abstract class
protected int height,width;
public abstract void paint();
public void setSize(int w, int h)
{
height=h;
width=w;
}
}
class MyButton extends AbstractComponent
{
public void paint()
{
System.out.println("Paint");
}
}
class AbstractDemo
{
public static void main(String[] args)
{
MyButton b1 = new MyButton();
b1.setSize(500,500);
b1.paint();
}
}Matlab- Fibonacci Series
n=input('Pick a number:\n');
fib=zeros(1,n);
fib(1)=1;
fib(2)=1;
k=3;
while k<=n
fib(k)=fib(k-2)+fib(k-1);
k=k+1;
end
fprintf('The fibonacci series to %d terms is \n',n);
fprintf('%g\n',fib);
Matlab- Prime Number
Write
a script to check whether entered number is prime number or not. If number is
prime then print its square value.
n=input('Pick
a number:\n');
count=[2:1:n+5];
k=1;
c=0;
while
count(k)~=n
if(rem(n,count(k))~=0)
k=k+1;
else
c=c+1;
end
end
if
c==0
fprintf('%d is prime',n);
fprintf('and its square is %d ',n^2);
else
fprintf('%d is not prime',n);
end
OUTPUT
Pick a
number:
3
3 is
prime and its square is 9
Matlab-Number of distinct elements in a matrix
Write
a script program to display the number of distinct elements in a matrix?
n=input('Enter
number of rows you want in matrix:\n');
m=input('Enter
number of columns you want in the matrix:\n');
a=input('Enter
a matrix:');
r=1;
c=1;
count=0;
b=reshape(a,1,n*m);
while
c<m*n
if b(1,c)~=b(1,c+1)
count=count+1;
end
c=c+1;
end
disp(count);
OUTPUT
Enter
number of rows you want in matrix:
2
Enter
number of columns you want in the matrix:
3
Enter
a matrix:[1,1,2;3,4,5]
5
Subscribe to:
Comments (Atom)

