Professional Documents
Culture Documents
Exercise 20:
Write a program that transposes words in a given sentence. For example,
given an input sentence
The gate to Java nirvana is near
the method outputs
ehT etag ot avaJ anavrin si raen
Answer:(8 marks)
package chapter9_Exercises;
import java.util.*;
}
}
Exercise 22
Write a program that determines if an input word is a palindrome. A palindrome is a string that reads
the same forward and backward, for example, noon and madam. Ignore the case of the letter. So, for
example, maDaM, MadAm, and mAdaM are all palindromes. Repeat the operation until an empty
string is entered.
import java.util.*;
if(isPalindrome(input))
{
System.out.println(input+" is a palindrome string");
}
else
{
System.out.println(input+" is not palindrome string");
}
}
}
str = str.toLowerCase();
Question: Write a program to compute the factorial of N. The factorial of N is the product of the
first N positive integers, denoted mathematically as
N! = 1 * 2 * ... * (N-2) * (N-1) * N
Repeat the operation until -1 is entered.
Example:
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
import java.util.*;
public class FactorialExample {
int fact=1;
Answer:
import java.util.*;
public class FibonacciExample {
**********************************************************
Chapter 14 – GUI (16 Marks)
Example (1)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JTextField txtName;
JTextField txtAge;
JComboBox semester;
JCheckBox[] chklanguage;
JTextArea detail;
JButton ok;
JButton cancel;
public GUIExample_One()
{
setTitle("GUI Example");
setSize(500,500);
setLocation(150,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Label
lblName = new JLabel("Name");
lblAge = new JLabel("Age");
lblSemester = new JLabel("Semester");
lblLanguage = new JLabel("Can Program in :");
// TextBox
txtName = new JTextField();
txtAge = new JTextField();
// ComboBox
String[] semesterText = {"Semester V", "Semester VI", "Semester VII", "Semester
VIII"};
semester = new JComboBox(semesterText);
// CheckBox
JPanel checkPanel = new JPanel(new FlowLayout());
checkPanel.add(lblLanguage);
String[] checkboxText = {"C++", "Java", "C#", "Python"};
chklanguage = new JCheckBox[checkboxText.length];
for (int i = 0; i < chklanguage.length; i++) {
chklanguage[i] = new JCheckBox(checkboxText[i]);
checkPanel.add(chklanguage[i]);
}
first.add(lblSemester);
first.add(semester);
first.add(checkPanel);
first.add(detail);
contentpane.add(first, BorderLayout.CENTER);
contentpane.add(buttonpanel, BorderLayout.SOUTH);
}
// ComboBox
sem = (String) semester.getSelectedItem();
// CheckBox
language = "";
for (int i = 0; i < chklanguage.length; i++) {
if (chklanguage[i].isSelected()) {
language += chklanguage[i].getText() + "\n ";
}
}
detail.setText("Detail Information: \n");
detail.append(name + "\n" + age +"\n"+ sem + "\n"+ language);
}
}
********************************************************
Example (2)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JTextField txtName;
JTextField txtAge;
JRadioButton[] radgender;
JList arealist;
JLabel detail;
JButton ok;
JButton cancel;
public GUIExample_Two()
{
setTitle("GUI Example");
setSize(500,500);
setLocation(150,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Label
lblName = new JLabel("Name");
lblAge = new JLabel("Age");
lblGender = new JLabel("Gender");
lblInternship = new JLabel("Internship Area");
// TextBox
txtName = new JTextField();
txtAge = new JTextField();
// RadioButton
String[] genderText = {"Male", "Female"};
JPanel radioPanel = new JPanel(new GridLayout(1,1));
// List
String[] areaText = {"Programming", "Networking", "Database", "Accounting"};
arealist = new JList(areaText);
arealist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
contentpane.add(first, BorderLayout.CENTER);
contentpane.add(buttonpanel, BorderLayout.SOUTH);
}
// RadioButton
gender = "";
for (int i = 0; i < radgender.length; i++) {
if (radgender[i].isSelected()) {
gender = radgender[i].getText() + "\n ";
}
}
// List
Object[] list = arealist.getSelectedValues();
String area_select = "";
for (int i = 0; i < list.length; i++) {
area_select += (String)list[i]+" ";
}
detail.setText("Detail Information : "+ name + " " + age + " " + gender + ":
interest in "+ area_select);
}
}
}