Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


nullP
java
2 Answer(s)      2 years and 2 months ago
Posted in : Java Beginners


How to restrict the object of a class upto 10.If objects exceeds the limit though "LimitCrossed" Exception.plz rply code urgent.....


Advertisement
View Answers

October 25, 2012 at 2:12 PM


public class Arm {
    public static void main(String[] args) throws Exception {
        for (int k = 10; k <= 1000; k++) {
            int n = k;
            int d = 0;
            int s = 0;

            while (n > 0) {
                d = n % 10;
                s = s + (d * d * d);
                n = n / 10;
            }
            if (k == s) {
                System.out.println(k + " is Armstrong number");
            }
        }
    } 
}

OutPut:

153 is Armstrong number
370 is Armstrong number
371 is Armstrong number
407 is Armstrong number


October 17, 2012 at 7:41 PM


hi, Here I am giving a code which will restrict the object creation upto a fix number of times. And this will throw an exception if the object creation of a class is exceeded a fix number of times.

Here I am giving a sample code for detail visit the following url http://www.roseindia.net/java/limitclassobjectcreation.shtml

ClassInstance.java

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public final class ClassInstance {

private static ClassInstance ci;
private static int counter=0;

private ClassInstance() {
counter++; 
}
public static ClassInstance getClassInstance() {
if(counter<5)
{
ci = new ClassInstance();
}
else if(counter >=5)
{
ci = new ClassInstance();
ci=null; 
} 
return ci;
}

public static void main(String[] args)
{
try{

List<ClassInstance> list = new ArrayList<ClassInstance>();
ClassInstance obj1 = ClassInstance.getClassInstance();
ClassInstance obj2 = ClassInstance.getClassInstance();
ClassInstance obj3 = ClassInstance.getClassInstance();
ClassInstance obj4 = ClassInstance.getClassInstance();
ClassInstance obj5 = ClassInstance.getClassInstance();
ClassInstance obj6 = ClassInstance.getClassInstance();
ClassInstance obj7 = ClassInstance.getClassInstance();
ClassInstance obj8 = ClassInstance.getClassInstance();
list.add(obj1);
list.add(obj2);
list.add(obj3);
list.add(obj4);
list.add(obj5);
list.add(obj6);
list.add(obj7);
list.add(obj8);
ListIterator<ClassInstance> itr = list.listIterator();
while(itr.hasNext())
{ 
System.out.println(itr.next().hashCode()); 
}
}
catch(Exception e)
{
try
{
if(counter >= 5)
{
MyException.counter = counter;
}
throw new MyException();
}
catch(MyException e1)
{
System.out.println("Exception@ "+ClassInstance.class+" : "+e1);
}
}
}
}


Related Tutorials/Questions & Answers:
java
java  diff bt core java and java
java
java  what is java
Advertisements
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
Java
Java  how to draw class diagrams in java
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java  is java purely object oriented language
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  Does java allows multiline comments
 

 

 

DMCA.com