You are on page 1of 9

For Exam - Revision

Chapter 9 – Character and String

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.*;

public class Exercise_20 {

public static void main(String[] args) {


String inputSentence;
StringBuffer strBuffer;
Scanner scanner = new Scanner(System.in);
while (true)
{
System.out.print("\nEnter a sentence : ");
inputSentence = scanner.nextLine();
if(inputSentence.length() == 0)
break;

StringTokenizer st = new StringTokenizer(inputSentence," ");


while (st.hasMoreTokens()) {
strBuffer = new StringBuffer(st.nextToken());
strBuffer.reverse();
System.out.print(strBuffer + " ");
}
}

}
}
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.*;

public class PalindromeExample {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
while(true)
{
System.out.print("Enter a word to check palindrome: ");
String input = scanner.nextLine();
if(input == "")
break;

if(isPalindrome(input))
{
System.out.println(input+" is a palindrome string");
}
else
{
System.out.println(input+" is not palindrome string");
}
}
}

public static boolean isPalindrome(String str) {


int left = 0;
int right = str.length()-1;

str = str.toLowerCase();

while(left < right)


{
if(str.charAt(left) != str.charAt(right))
{
return false;
}
left++;
right--;
}
return true;
}
}
Chapter 6 - Repetition Statements

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 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


while(true)
{
System.out.print("Enter a number to find factorial:");
int number = scan.nextInt();
if(number == -1)
break;

int fact=1;

for(int i=1; i <= number; i++)


{
fact = fact * i;
}
System.out.println("Factorial of "+ number+" is: "+ fact);
}
}
}
Question: Write a program that accepts N, N >=1, from the user and displays the first N numbers in
the Fibonacci sequence. Use appropriate formatting to display the output cleanly.
For Example:
F1 = 1
F2 = 1
F3 = F2 + F1 = 1 + 1 = 2
F4 = F3 + F2 = 2 + 1 = 3
F5 = F4 + F3 = 3 + 2 = 5
F6 = F5 + F4 = 5 + 3 = 8
F7 = F6 + F5 = 8 + 5 = 13
F8 = F7 + F6 = 13 + 8 = 21
F9 = F8 + F7 = 21 + 13 = 34
F10 = F9 + F8 = 34 + 21 = 55

Answer:

import java.util.*;
public class FibonacciExample {

public static void main(String[] args) {


int n1 = 1;
int n2 = 1;
int n3;

Scanner scanner = new Scanner(System.in);


System.out.print("Enter N numbers in the Fibonacci sequence: ");
int N = scanner.nextInt();

System.out.println("Fibonacci Sequence is : ");


System.out.print(n1 +" "+ n2);

for(int i=2; i<N; i++)


{
n3 = n1 + n2;
System.out.print(" " + n3);
n1 = n2;
n2 = n3;
}
}
}

**********************************************************
Chapter 14 – GUI (16 Marks)

Example (1)

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

public class GUIExample_One extends JFrame implements ActionListener{


JLabel lblName;
JLabel lblAge;
JLabel lblSemester;
JLabel lblLanguage;

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);

Container contentpane = getContentPane();


contentpane.setLayout(new BorderLayout());

// 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]);
}

detail = new JTextArea();

JPanel first = new JPanel(new GridLayout(0,2));


first.add(lblName);
first.add(txtName);
first.add(lblAge);
first.add(txtAge);

first.add(lblSemester);
first.add(semester);
first.add(checkPanel);

first.add(detail);

JPanel buttonpanel = new JPanel(new FlowLayout());


ok = new JButton("OK");
ok.addActionListener(this);
buttonpanel.add(ok);
cancel = new JButton("CANCEL");
cancel.addActionListener(this);
buttonpanel.add(cancel);

contentpane.add(first, BorderLayout.CENTER);
contentpane.add(buttonpanel, BorderLayout.SOUTH);
}

public static void main(String[] args) {


GUIExample_One fobj = new GUIExample_One();
fobj.setVisible(true);

public void actionPerformed(ActionEvent e) {


String name, age, sem, language;
if(e.getSource().equals(cancel))
{
System.exit(0);
}
if(e.getSource().equals(ok))
{
// TextBox
name = txtName.getText();
age = txtAge.getText();

// 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.*;

public class GUIExample_Two extends JFrame implements ActionListener{


JLabel lblName;
JLabel lblAge;
JLabel lblGender;
JLabel lblInternship;

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);

Container contentpane = getContentPane();


contentpane.setLayout(new BorderLayout());

// 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));

ButtonGroup languageGroup = new ButtonGroup();


radgender = new JRadioButton[genderText.length];
radioPanel.add(lblGender);
for (int i = 0; i < radgender.length; i++) {
radgender[i] = new JRadioButton(genderText[i]);
languageGroup.add(radgender[i]);
radioPanel.add(radgender[i]);
}
radgender[0].setSelected(true);

// List
String[] areaText = {"Programming", "Networking", "Database", "Accounting"};
arealist = new JList(areaText);

arealist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

detail = new JLabel();

JPanel first = new JPanel(new GridLayout(0,2));


first.add(lblName);
first.add(txtName);
first.add(lblAge);
first.add(txtAge);
first.add(radioPanel);
first.add(lblInternship);
first.add(arealist);
first.add(detail);
JPanel buttonpanel = new JPanel(new FlowLayout());
ok = new JButton("OK");
ok.addActionListener(this);
buttonpanel.add(ok);
cancel = new JButton("CANCEL");
cancel.addActionListener(this);
buttonpanel.add(cancel);

contentpane.add(first, BorderLayout.CENTER);
contentpane.add(buttonpanel, BorderLayout.SOUTH);
}

public static void main(String[] args) {


GUIExample_Two fobj = new GUIExample_Two();
fobj.setVisible(true);
}

public void actionPerformed(ActionEvent e) {


String name, age, gender;
if(e.getSource().equals(cancel))
{
System.exit(0);
}
if(e.getSource().equals(ok))
{
// TextBox
name = txtName.getText();
age = txtAge.getText();

// 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);
}
}
}

You might also like