Sie sind auf Seite 1von 18

TABLE OF CONTENTS

Page No.

I. Problem Description and Analysis ...................


…………………... …..... 1

II. Data Structure Implementation . . .. . . . .. .. . . . . . . . . . . . . . . . . . . . . . . .


...... 2–4

III. Graphical User Interface Design Layout . . . . . . . . . . . . . . . . . . . .. . . . . . . . . .


....... 5–7

IV. Simulation Results .. .. . . . . . . . . . . . . . . . . . . . . . . .


8 - 10

V. Discussion and Recommendation . . . . . . . . . . . . . . . 11 - 16

VI. Code Solution for the Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


....... 17 - 20

VII. Analysis of Complexity of the Algorithm ...........................


....... 21 - 23

I
I] PROBLEM DESCRIPTION AND ANALYSIS

The given problem given to us is a discrete mathematics professor has a class of N


students. Frustrated with their lack of discipline, he decides to cancel classes if fewer that K
students are present when class starts. Given the arrival time of each student, determine if the
class is cancel. The first line of input contains T, the number of the test cases. Each test case
consist of two lines. The first line has two space-separates integers, N (students in the class) and
K ( the cancellation threshold). the second line contains N space-separated integers describing
the arrival times for each student,

Constraints
 1≤ T ≤ 10
 1≤ T ≤ 1000
 1≤ T ≤ N
 -100 ≤ ai ≤ 100, where i  [1, N]

1. A B C D K=3

-1 -3 4 2

Arrival time(m)

2. A B C D K=3

-1 -3 4 2

Arrival time(m)
II] DATA STRUCTURE IMPLEMENTATION

Data Structure is a way of collecting and organizing data in such a way that we can perform
operations on these data in an effective way. Data Structure is about rendering data elements in
terms of some relationship,for better organization and storage.

The problem that is given to us is to figure the students arrival time to determine if the
class will start or canceled. Given a sequence of number from 0 to - which means the students
arrived early in the class and for students who are late is 0 to + in that we will know if the class
will start or canceled. The system will show a dialog after the calculation of students arrival time.

As we do some research, we come up to solved the problem by using the Linked List as
our data structure. As we know Linked List is a linear data structure where each element is a
separate object. Each node of a list is made up two items the data and a reference to the next
node.This help us to create our system lot easier.
III] GRAPHICAL USER INTERFACE DESIGN LAYOUT

This is the user interface design of our system

The dialog message after calculation of the students arrival


IV] SIMULATION RESULTS

This is automated system using java language. Pictures below will show the results and process
involved in solving the project. It will also test how the program runs and behave by inputting
different inputs.

This is the system looks like in adding the student and the records
This the dialog show after the calculation of the students arrival and if the class will start

V] DISCUSSION AND RECOMMENDATION


We recommend to used database management system. A database management system
is a software tool that makes it possible to organize data in a database. The functions of a
database include concurrency, security,backup and recovery, integrity and data descriptions. By
the help of database management system, it is now much easier to access and retrieve the data.
We can also add more accessories or design such as time and date. Our system is user- friendly
and we are expecting that it is easier for them to access.
VI] CODE SOLUTION FOR THE PROBLEM

This is the user interface design of our system

The dialog message after calculation of the students arrival


VII] ANALYSIS OF COMPLEXITY OF THE ALGORITHM

Given the program. You can input you data, the name of the student and there time arrival

Given a number N, calculate the the students time arrival and determine if the class will be cancel
or will start

 1≤ T ≤ 10
 1≤ T ≤ 1000
 1≤ T ≤ N
 -100 ≤ ai ≤ 100, where i  [1, N]

3. A B C D K=3

-1 -3 4 2

Arrival time(m)

4. A B C D K=3

-1 -3 4 2

Arrival time

VII] ANALYSIS OF COMPLEXITY OF THE ALGORITHM


JAVA CODE ANALYSIS

DATABASE
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class DATABASE{
private File myFile;
private FileWriter fWrite;
private FileReader fRead;
private Scanner read;
private Vector row;
private String filename;
private String data[];

public DATABASE() {
filename="GGME.txt";
myFile=new File(filename);}
public DATABASE(String filename) {
this.filename=filename;
myFile=new File(filename);} 1(0)
public void setFilename(String filename) {
this.filename = filename;
myFile=new File(filename); }
public String getFilename() {
return filename;} 0(n)
public void addRecords(String records){
try {fWrite=new FileWriter(myFile);
fWrite.write(records);
fWrite.flush();
} catch (Exception e) {
errorMessage("Error 101: addRecords\n"+e.getMessage());
e.printStackTrace(); }}public Vector setColumns(){ 0(n)
Vector column=new Vector();
try { fRead=new FileReader(myFile);
read=new Scanner(fRead);
while(read.hasNext()){
column.add(read.nextLine()); }
fRead.close();
read.close();
} catch (Exception e) {
errorMessage("Error 102: setColumns\n"+e.getMessage());
e.printStackTrace(); }return column; }
public void displayRecords(DefaultTableModel model){
try { fRead=new FileReader(myFile);
read=new Scanner(fRead);
while(read.hasNext()){ (n)
data=read.nextLine().split("#");
model.addRow(data); }
fRead.close();
read.close();
} catch (Exception e) {
errorMessage("Error 103: displayRecords\n"+e.getMessage());
e.printStackTrace(); }}
public void loadToComboBox(JComboBox cboData){
try { fRead=new FileReader(myFile); 0(n)
read=new Scanner(fRead);
while(read.hasNext()){
cboData.addItem(read.nextLine()); }
fRead.close();
read.close(); } catch (Exception e) {
errorMessage("Error 104: loadtocombobox\n"+e.getMessage());
e.printStackTrace(); }}
public void errorMessage(String error){ System.err.println(error);}}
MAIN
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import javax.swing.table.TableRowSorter;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import javax.swing.table.TableRowSorter;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import javax.swing.table.TableRowSorter;
public class CodeJ extends JFrame implements ActionListener,KeyListener,MouseListener {
JLabel lblId, lblName, lblRecord;
JTextField txtName, txtId, txtRecord;
JButton btnBack, btnDelete, btnUpdate, btnAddStudent, btnAdd, btnBack2, btnCal;
JComboBox cboFrom1, cboTripStatus, cboAirline, cboSeatClass, cboTo1;
TableRowSorter tblsort;
JPanel panelMainmenu,panelAddStudent, panelCal; JTable tblAddTable;
DefaultTableModel modelAddTable;
DATABASE Recs = new DATABASE();
String colomn [ ] = {"ID", "Name", "Record",}; Vector row;
CodeJ(){ lblId = new JLabel ("ID: ");
lblName = new JLabel ("Name: ");
lblRecord = new JLabel ("Record: ");
txtName = new JTextField();
txtId = new JTextField();
txtRecord = new JTextField();
btnBack = new JButton("Back");
btnDelete = new JButton ("Delete");
btnCal = new JButton ("Calculate");
btnUpdate = new JButton ("Update");
btnAddStudent = new JButton ("Add Student");
btnAdd = new JButton ("Add");
panelMainmenu = new JPanel ();
panelAddStudent = new JPanel ();
panelCal = new JPanel ();
tblsort = new TableRowSorter();
modelAddTable = new DefaultTableModel();
modelAddTable.setColumnIdentifiers(colomn);
tblAddTable = new JTable(modelAddTable);
Recs.displayRecords(modelAddTable);}
public void setWindow() {panelMainmenu.setLayout(new GridLayout(2 ,2)); n(0)
panelMainmenu.setBorder(BorderFactory.createTitledBorder(null, "Main Menu",
TitledBorder.CENTER, 2));
panelMainmenu.add(btnAddStudent);panelMainmenu.add(btnDelete);
panelMainmenu.add(btnUpdate);panelMainmenu.add(btnBack);
panelAddStudent.setLayout(new GridLayout(4 ,2));
panelAddStudent.setBorder(BorderFactory.createTitledBorder(null, "Record",
TitledBorder.CENTER, 3));
panelAddStudent.add(lblId);panelAddStudent.add(txtId);
panelAddStudent.add(lblName);panelAddStudent.add(txtName);
panelAddStudent.add(lblRecord);panelAddStudent.add(txtRecord);
panelAddStudent.add(btnAdd);
panelCal.setLayout(new GridLayout(1 ,1));
panelCal.add(btnCal); setLayout(null);
add(panelCal).setBounds(10,380,280,50);
add(panelMainmenu).setBounds(10, 10, 280, 130);
add(new JScrollPane(tblAddTable)).setBounds(310, 30, 650, 400);
add(panelAddStudent).setBounds(10, 150, 280, 150);
setTitle("Student's Records");
setSize(1000,500); setResizable(false);
setLocationRelativeTo(null); setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
panelAddStudent.setVisible(false);
btnAddStudent.setVisible(true);
btnDelete.addActionListener(this );
btnAddStudent.addActionListener(this);
btnAdd.addActionListener(this);
btnBack.addActionListener(this);
btnCal.addActionListener(this);
btnUpdate.addActionListener(this);
txtName.addKeyListener(this);
txtRecord.addKeyListener(this);
tblAddTable.addMouseListener(this);}
public void getValues () {
row = new Vector ();
row.add(txtId.getText());
row.add(txtName.getText());
row.add(txtRecord.getText());
modelAddTable.addRow(row);
int a = tblAddTable.getRowCount() - 1;
String i = tblAddTable.getValueAt(a, 0) + "";
int c = Integer.parseInt(i) + 1;
txtId.setText(c + "");}
public void setValues(Vector value){
txtId.setText(value.get(0).toString());
txtName.setText(value.get(1).toString());
txtRecord.setText(value.get(2).toString());}
public void XYZ() { String records = "";
for (int row = 0; row < tblAddTable.getRowCount(); row++){
for (int column = 0; column < tblAddTable.getColumnCount(); column++){ 0(1)
records+=tblAddTable.getValueAt(row, column)+"#"; }
records+="\n"; } Recs.addRecords(records); } @Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(btnAddStudent)){ 0(1)
panelAddStudent.setVisible(true); 0(n)
btnAddStudent.setEnabled(true);
}else if (e.getSource().equals(btnBack)){ 0(1)
panelAddStudent.setVisible(false); panelMainmenu.setVisible(true);
}else if (e.getSource().equals(btnCal)){ int total=0; 0(1)
for (int row = 0; row<tblAddTable.getRowCount();row++) { 0(1)
Object value = tblAddTable.getValueAt(2,row);
if (value != null) { total+=Integer.parseInt((String)value); } 1(0)
JOptionPane.showMessageDialog(null,total>=0? "Class Starts": "Class Cancel" , "
",JOptionPane.INFORMATION_MESSAGE); XYZ(); };
}else if(e.getSource().equals(btnUpdate)){ try { 1(0)
int row = tblAddTable.getSelectedRow(); 0(1)
btnAddStudent.setEnabled(false); 0(1)
btnDelete.setEnabled(false); 0(1)
btnBack.setEnabled(false); 0(1)
}catch(ArrayIndexOutOfBoundsException t){ } XYZ();} 0(1) 0(n)
else if(e.getSource().equals(btnAdd)); 0(1)
panelAddStudent.setVisible(false); 0(1)
tblAddTable.setVisible(true); 0(1)
getValues(); XYZ();}
else if(e.getSource().equals(btnDelete)){ 0(1)
int row = tblAddTable.getSelectedRow(); 0(1)
modelAddTable.removeRow(tblAddTable.convertRowIndexToModel(row)); XYZ(); }
else if (e.getSource().equals(btnBack)) { }} 0(1)
public void keyTyped(KeyEvent e) { 0(1)
if(e.getSource().equals(txtRecord)){ 0(1)
if(e.getKeyChar()>='a' && e.getKeyChar()<='z'){ e.consume(); }
if (e.getSource().equals(txtId)){ 0(1)
if(e.getKeyChar()>='a' && e.getKeyChar()<='z'){
e.consume(); }}} public void keyPressed(KeyEvent e) { }
public void keyReleased(KeyEvent e) { } @Override
public void mouseClicked(MouseEvent k) {
if(k.getSource().equals(tblAddTable)) { 0(1)
int row1 = tblAddTable.getSelectedRow();
panelMainmenu.setVisible(true); 0(1)
btnAddStudent.setEnabled(true); 0(1)
btnDelete.setEnabled(true); 0(1)
btnBack.setEnabled(true); 0(1)
btnUpdate.setEnabled(true); 0(1)
}else if(k.getSource().equals(txtName)){ 0(1)
txtName.setText(""); 0(1)
}else if(k.getSource().equals(txtRecord)){ 0(1)
txtRecord.setText(""); }}@Override
public void mouseEntered(MouseEvent gm) { }@Override
public void mouseExited(MouseEvent gm) { @Override
public void mousePressed(MouseEvent gm) {
if(gm.getSource().equals(txtName)){
if(txtName.getText().equals("")){
txtName.setText("N/A"); } }
if(gm.getSource().equals(txtRecord)){
if(txtRecord.getText().equals("")){
txtRecord.setText("N/A"); } } } @Override
public void mouseReleased(MouseEvent gm) { } }
public class MAINSYSTEM {
public static void main (String [] args) { CodeJ j = new CodeJ(); j.setWindow(); }}
COMPLEXITY OF THE ALGORITHM:

The overall complexity of the algorithm is linear, O(n). Then, to prove using mathematical
induction that for all values of n starting n = 1, it is true to n = n+1 and the succeeding values of n
such as n+2, n+3, . . ., and so on.

PROOF USING MATHEMATICAL INDUCTION

Complexity of the Algorithm: O(n)

𝒏(𝒏+𝟏)
O(n) = ∑𝒏𝒊=𝟏 𝒊 =
𝟐

𝑛(𝑛+1)
Prove that, for any natural number n≥ 1, 1 + 2 + 3 +. . . + n = .
2

Solution:

Base case: Case n = 1.

1(1+1) 1(2) 2
∑𝑛𝑖=1 𝑖 = = = =1
2 2 2

𝑘(𝑘+1)
Inductive step: Assume that the identity holds for the case n = k, i.e.,∑𝑘𝑖=1 𝑖 = 2
.

Next, we show that the statement is true for n = k+1; that is, we have to show that

L.H.S. (Left-Hand-Side) R.H.S. (Right-Hand-Side)

𝑘+1(𝑘+1+1) (𝑘+1)(𝑘+2)
1 + 2 + 3 + ⋯ + 𝑘 + (𝑘 + 1) = =
2 2

Consider the L.H.S (Left Hand Side)

𝑘(𝑘+1)
Since, 1+2+3+…+k = 2
, 𝑡ℎ𝑒𝑛

𝑘(𝑘+1)
Replace, 1 + 2 + 3 + … + k at L.H.S. with 2

Continuing, L.H.S.

𝑘(𝑘+1) 𝑘(𝑘+1)+2(𝑘+1)
+ (𝑘 + 1) =
2 2

Factor out (k+1)

(𝑘+1)(𝑘+2)
= 2
. Hence, proving the theorem.

Das könnte Ihnen auch gefallen