Lab Assignment 3 - CSF206 Advanced Java Programming, Even Sem, 2022

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Lab Assignment 3 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID:1000014908 Major:CSE

Instructions
 In this lab assignment, you will solve problem(s) based on what you have learnt in
GUI.
 There are 2 questions in this assignment.
 Email/paper/other modes of submissions will not be accepted.
 Upload a pdf version of this document.

Due Date: 10 pm, Feb 4, 2022.

Submitting this Assignment

You will submit (upload) this assignment in MS Teams. Name this document as
A3_AJPEVEN2022_John_Doe.pdf in case your name is John Doe. Paste your code and
snapshot of output after each question, save and upload the document.

Grading: 0 points. Submission for each student is mandatory.

Question 1: Write a program that has a method commonPre (String str1, String str2) that
returns the common prefix of two strings. For example, the common prefix of “distance”
and “disinfection” is “dis”. If the two strings have no common prefix, the method displays
“No common prefix”. The user should be prompted to enter two strings in JOptionPane and
display their common prefix, if any. Use JOptionPane for all inputs and outputs.

Question 2: Using FlowLayout Manager, write a program that meets the following require-
ments (see figure below):
 Create a frame and set its layout to FlowLayout
 Create 2 panels and add them to the frame.
 Each panel contains three buttons. The panel uses FlowLayout.
Lab Assignment 3 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID:1000014908 Major:CSE

Question 1 code:
package com.java;

import javax.swing.JOptionPane;

public class App {

public static void main(String[] args) {


String str1 = JOptionPane.showInputDialog("Enter the string 1: ");
String str2 = JOptionPane.showInputDialog("Enter the string 2: ");
JOptionPane.showMessageDialog(null, commonPre(str1, str2));
}

private static String commonPre(String str1, String str2) {


int minLen = str1.length();
if (minLen > str2.length()) {
minLen = str2.length();
}
String commonPrefix = "";
for (int i = 0; i < minLen; i++) {
if (str1.charAt(i) == str2.charAt(i)) {
commonPrefix += str1.charAt(i);
} else {

if (commonPrefix.length() > 0) {
return commonPrefix;
} else {
break;
}
}
}
if (commonPrefix.length() > 0) {
return commonPrefix;
}
return "No common prefix. :(";
}
}

OUTPUT:
Lab Assignment 3 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID:1000014908 Major:CSE

Question 2 code:
Lab Assignment 3 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID:1000014908 Major:CSE

package com.java;
import javax.swing.*;
import java.awt.*;

public class flowlayout extends JFrame {

public flowlayout() {
setLayout(new FlowLayout());

this.setLayout(new FlowLayout());

JPanel panel1 = new JPanel();


JPanel panel2 = new JPanel();

panel1.setLayout(new FlowLayout());
panel2.setLayout(new FlowLayout());

panel1.add(new JButton(" Button 1 "));


panel1.add(new JButton(" Button 2 "));
panel1.add(new JButton(" Button 3 "));
panel2.add(new JButton(" Button 4 "));
panel2.add(new JButton(" Button 5 "));
panel2.add(new JButton(" Button 6 "));

this.add(panel1);
this.add(panel2);
}

public static void main(String[] args) {


flowlayout frame = new flowlayout();
frame.setTitle(" Exercise 12_1 ");
frame.setSize(600, 100);
frame.setLocationRelativeTo(null); // center frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:
Lab Assignment 3 – CSF206 Advanced Java Programming, Even Sem, 2022

Your Name: Madhava Yedla SAP ID:1000014908 Major:CSE

You might also like