Q1)
You can get the program here.
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class StringButtons extends JFrame {
5: public static void main(String[] args) {
6: JFrame f = new JFrame();
7: f.getContentPane().setLayout(new GridLayout(1,0));
8: for(int i = 0; i < args.length; i++) {
9: f.getContentPane().add(new JButton(args[i]));
10: }
11: f.pack();
12: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13: f.setVisible(true);
14: }
15:}
Q2)
You can get the program here.
1:public class StringLength {
2: public static void main(String[] args) {
3: int sum = 0;
4: for(int i = 0; i < args.length; i++) {
5: sum+=args[i].length();
6: }
7: System.out.println(sum);
8: }
9:}
Q3) You can get the program here.
1:public class RemoveDigit {
2: public static String removeDigit(String s) {
3: char[] array = s.toCharArray();
4: String temp = "";
5: for(int i = 0; i < array.length; i++) {
6: if(!Character.isDigit(array[i])) {
7: temp = temp + array[i];
8: }
9: }
10: return temp;
11: }
12:
13: public static void main(String[] args) {
14: for(int i = 0; i < args.length; i++) {
15: System.out.print(removeDigit(args[i]) + " ");
16: }
17: }
18:}
Q4) You can get the program here.
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class NumberLabels extends JPanel{
5: public static int HORIZONTAL = 0;
6: public static int VERTICAL = 1;
7: public NumberLabels(int orientation) {
8: if(orientation == VERTICAL) {
9: setLayout(new GridLayout(0,1));
10: } else {
11: setLayout(new GridLayout(1,0));
12: }
13: for(int i = 1; i <= 9; i++) {
14: add(new JLabel(Integer.toString(i)));
15: }
16: }
17:
18:}
19:
Q5) string.length() and array.length are correct.
Q6) Refer to my notes.
Q7) (i),(iii),(iv). are correct.
(ii) begins with a digit, which is not allowed
(v) and (vi) contains characters other than _, $
(vii) is a reserved keyword.