OutBounds

Ads

 
 

Share on Google+Share on Google+

Rajbir Bhullar
OutBounds
1 Answer(s)      4 years and 5 months ago
Posted in : Java Beginners

Write a program called OutBounds that meets the following requirements: Create a randomly sized int array with random numbers. Create a text field to enter an aray index and another textfield to display the array element at the specified index Create a 'Show Element' button to cause the array element to be displayed. Catch out-of-bounds exceptions and display "Out Of Bounds" in the array element field.

Ads
View Answers

December 6, 2012 at 11:39 AM


Here is an example that creates a randomply sized int array with random numbers.

import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

class ShowElement extends JFrame{
JButton ADD;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
ShowElement(){
label1 = new JLabel();
label1.setText("Enter Index:");
text1 = new JTextField(20);

label2 = new JLabel();
label2.setText("Element at specific Index:");
text2 = new JTextField(20);

ADD=new JButton("Show Element");

panel=new JPanel(new GridLayout(3,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(ADD);
add(panel,BorderLayout.CENTER);
Random r=new Random(); 
final int size=r.nextInt(50)+1;
final int arr[]=new int[size];
for(int i=0;i<arr.length;i++){
    int x=r.nextInt(100)+1;
    arr[i]=x;
    System.out.println(arr[i]);
}
ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
    try{
    String value=text1.getText();
int in=Integer.parseInt(value);
if(in>=size){
    JOptionPane.showMessageDialog(null,"Out of Bounds");
}
else{
int ele=0;
for(int i=0;i<arr.length;i++){
    if(i==in){
        ele=arr[i];
    }
}

text2.setText(Integer.toString(ele));
}
    }
    catch(Exception e){}
    }
});
}

public static void main(String arg[])
{
try
{
ShowElement frame=new ShowElement();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e){
}
}
}

Ads









Related Tutorials/Questions & Answers:
OutBounds
OutBounds  Write a program called OutBounds that meets the following requirements: Create a randomly sized int array with random numbers. Create a text field to enter an aray index and another textfield to display the array
Tutorials   
Java Spring Hibernate Struts Training What type of programming language is Java? How do beginners learn about Java? ClassNotFoundException HttpRequestInterceptor java.lang.noclassdeffounderror: org/apache/http/httprequest noclassdeffounderror: org/apache/http/client/methods/httpurirequest java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017

Ads

 
Advertisement null

Ads