diff --git a/src/ChatClient.java b/src/ChatClient.java index 1ab6271..9fcfd74 100644 --- a/src/ChatClient.java +++ b/src/ChatClient.java @@ -5,148 +5,182 @@ import java.awt.*; import java.awt.event.*; -public class ChatClient { - - JTextArea incoming; - JTextField outgoing; - JTextArea userList; - BufferedReader reader; - PrintWriter writer; - Socket sock; - String username; - String[] ps; - - public static void main(String[] args) - { - ChatClient client = new ChatClient(); - client.go(); - } - - public void go() - { - - JFrame frame = new JFrame("Client"); - JPanel mainPanel = new JPanel(); - JLabel l1, l2; - incoming = new JTextArea(15, 50); - incoming.setLineWrap(true); - incoming.setWrapStyleWord(true); - incoming.setEditable(false); - l1 = new JLabel("Message Box"); - JScrollPane qScroller = new JScrollPane(incoming); - qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); - qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); - - userList = new JTextArea(15, 10); - userList.setLineWrap(true); - userList.setWrapStyleWord(true); - userList.setEditable(false); - l2 = new JLabel("User List"); - JScrollPane uScroller = new JScrollPane(userList); - uScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); - - outgoing = new JTextField(20); - JButton sendButton = new JButton("Send"); - sendButton.addActionListener(new SendButtonListener()); - //mainPanel.add(l1); - //mainPanel.add(l2); - mainPanel.add(qScroller); - mainPanel.add(uScroller); - mainPanel.add(outgoing); - mainPanel.add(sendButton); - setUpNetworking(); - - username = JOptionPane.showInputDialog("Welcome ! Kindly say who you are ?"); - - frame.setTitle(username.toUpperCase()); - - Thread readerThread = new Thread(new IncomingReader()); - readerThread.start(); - - frame.getContentPane().add(BorderLayout.CENTER, mainPanel); - //frame.getContentPane().add(BorderLayout.CENTER, mainPanel); - frame.setSize(800, 400); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setVisible(true); - - } - - private void setUpNetworking() - { - - try - { - sock = new Socket("127.0.0.1", 6666); - InputStreamReader streamReader = new InputStreamReader(sock.getInputStream()); - reader = new BufferedReader(streamReader); - writer = new PrintWriter(sock.getOutputStream()); - System.out.println("Networking established"); - - }catch(IOException ex) - { - ex.printStackTrace(); - } - } - - public class SendButtonListener implements ActionListener - { - - public void actionPerformed(ActionEvent ev) - { - try { - - writer.println(username + " : " + outgoing.getText()); - writer.flush(); - - }catch(Exception ex) { - ex.printStackTrace(); - } - - outgoing.setText(""); - outgoing.requestFocus(); - } - } - - public class IncomingReader implements Runnable - { - public void run() { - String message; - - try { - - while((message = reader.readLine()) != null) { - if(!messageOrList(message)) { - //System.out.println("read" + message); - incoming.append( message + "\n"); - }else { - userList.setText(""); - for(int i = 1; i < ps.length; i++) - { - //System.out.println(ps[i]); - - userList.append(ps[i]+"\n"); - } - - } - - } - }catch(Exception ex) { - ex.printStackTrace(); - } - } - public boolean messageOrList(String ms) - { - ps = ms.split("\\,"); - - if(ps[0].equals("000..")) - { - return true; - }else - { - return false; - } - - - } - } + + +// CLASS: ChatClient +public class ChatClient +{ + + JTextArea incoming; + JTextField outgoing; + JTextArea userList; + BufferedReader reader; + PrintWriter writer; + Socket sock; + String username; + String[] ps; + + // FUNCTION: main + public static void main(String[] args) + { + ChatClient client = new ChatClient(); + client.go(); + } + + // FUNCTION: go + public void go() + { + + JFrame frame = new JFrame("Client"); + JPanel mainPanel = new JPanel(); + + JLabel l1, l2; + l1 = new JLabel("Message Box"); + l2 = new JLabel("User List"); + + incoming = new JTextArea(15, 50); + incoming.setLineWrap(true); + incoming.setWrapStyleWord(true); + incoming.setEditable(false); + + + JScrollPane qScroller = new JScrollPane(incoming); + qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); + + userList = new JTextArea(15, 10); + userList.setLineWrap(true); + userList.setWrapStyleWord(true); + userList.setEditable(false); + + + JScrollPane uScroller = new JScrollPane(userList); + uScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + + outgoing = new JTextField(20); + JButton sendButton = new JButton("Send"); + sendButton.addActionListener(new SendButtonListener()); + + //mainPanel.add(l1); + //mainPanel.add(l2); + mainPanel.add(qScroller); + mainPanel.add(uScroller); + mainPanel.add(outgoing); + mainPanel.add(sendButton); + + setUpNetworking(); + + username = JOptionPane.showInputDialog("Welcome ! Kindly say who you are ?"); + + frame.setTitle(username.toUpperCase()); + + Thread readerThread = new Thread(new IncomingReader()); + readerThread.start(); + + frame.getContentPane().add(BorderLayout.CENTER, mainPanel); + //frame.getContentPane().add(BorderLayout.CENTER, mainPanel); + frame.setSize(800, 400); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + + } + + // FUNCTION: setUpNetworking + private void setUpNetworking() + { + + try + { + sock = new Socket("127.0.0.1", 6666); + + InputStreamReader streamReader = new InputStreamReader(sock.getInputStream()); + + reader = new BufferedReader(streamReader); + writer = new PrintWriter(sock.getOutputStream()); + + System.out.println("Networking established"); + + } + catch(IOException ex) + { + ex.printStackTrace(); + } + } + + // CLASS: SendButtonListener + public class SendButtonListener implements ActionListener + { + + + // FUNCTION: actionPerformed + public void actionPerformed(ActionEvent ev) + { + try + { + writer.println(username + " : " + outgoing.getText()); + writer.flush(); + } + catch(Exception ex) + { + ex.printStackTrace(); + } + + outgoing.setText(""); + outgoing.requestFocus(); + } + } + + + // CLASS: IncomingReader + public class IncomingReader implements Runnable + { + + // FUNCTION: run + public void run() + { + String message; + + try + { + while((message = reader.readLine()) != null) + { + if(!messageOrList(message)) + { + //System.out.println("read" + message); + incoming.append( message + "\n"); + } + else + { + userList.setText(""); + for(int i = 1; i < ps.length; i++) + { + //System.out.println(ps[i]); + userList.append(ps[i] + "\n"); + } + } + } + } + catch(Exception ex) + { + ex.printStackTrace(); + } + } + + // FUNCTION: messageOrList + public boolean messageOrList(String ms) + { + ps = ms.split("\\,"); + + if(ps[0].equals("000..")) + { + return true; + } + else + { + return false; + } + + } + } + } diff --git a/src/ChatServer.java b/src/ChatServer.java index cb3ba4c..62d5063 100644 --- a/src/ChatServer.java +++ b/src/ChatServer.java @@ -5,204 +5,240 @@ import javax.swing.*; - - import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +// CLASS: ChatServer +public class ChatServer +{ + + JTextArea clientsMessage; + JTextArea userList; + ArrayList clientOutputStreams; + JButton sendButton; + JTextField smsToAll; + ArrayList uname = new ArrayList(); + ArrayList port = new ArrayList(); + + + // CLASS: ClientHandler + public class ClientHandler implements Runnable + { + + BufferedReader reader; + Socket sock; + //JTextArea clientsMessage; get nullPointerException for this line wasted one day + + // FUNCTION: ClientHandler + public ClientHandler(Socket clientSocket) + { + + try + { + sock = clientSocket; + + InputStreamReader isReader = new InputStreamReader(sock.getInputStream()); + reader = new BufferedReader(isReader); + } + catch(Exception ex) + { + ex.printStackTrace(); + } + } + + // FUNCTION: run + public void run() + { + + String message; + String[] parts = null; + boolean once = false; + + try + { + while((message = reader.readLine()) != null) + { + if(!once) + { + //System.out.println(sock.getPort()); + port.add(sock); + parts = message.split("\\:"); + //System.out.println(parts[0]); + uname.add(parts[0]); + once = true; + } + + //System.out.println("read" + message); + clientsMessage.append(message + "\n"); + tellEveryone(message); + showingClients(); + } + + uname.remove(parts[0]); + port.remove(sock); + showingClients(); + tellEveryone(parts[0].toUpperCase() + "(" + sock + ")" + "has left the conversation\n"); + clientsMessage.append(parts[0].toUpperCase() + "(" + sock + ")" + "has left the conversation\n"); + } + catch(Exception ex) + { + ex.printStackTrace(); + } + + } + } + + // CLASS: SendButtonListener + public class SendButtonListener implements ActionListener + { + + // FUNCTION: actionPerformed + @Override + public void actionPerformed(ActionEvent arg0) + { + clientsMessage.append("SERVER : " + smsToAll.getText() + "\n"); + tellEveryone("SERVER : " + smsToAll.getText()); + + smsToAll.setText(""); + smsToAll.requestFocus(); + } + + } + + // FUNCTION: showingClients + public void showingClients() + { + String st = "000..,"; + String t = ""; + + for(String s : uname) + { + st += s + ","; + t += s + "\n"; + } + + System.out.println(st); + + Iterator it = clientOutputStreams.iterator(); + userList.setText(t); + + while(it.hasNext()) + { + try + { + PrintWriter writer = (PrintWriter) it.next(); + writer.println(st); + writer.flush(); + } + catch(Exception ex) + { + ex.printStackTrace(); + } + } + } + + + // FUNCTION: go + public void go() + { + clientOutputStreams = new ArrayList(); + + try + { + ServerSocket serverSock = new ServerSocket(6666); + + while(true) + { + Socket clientSocket = serverSock.accept(); + //System.out.println(clientSocket.getPort()); + PrintWriter writer = new PrintWriter(clientSocket.getOutputStream()); + clientOutputStreams.add(writer); + + Thread t = new Thread(new ClientHandler(clientSocket)); + t.start(); + System.out.println("got a connection from " + clientSocket); + //showingClients(); + } + } + catch(Exception ex) + { + ex.printStackTrace(); + } + + } + + + // FUNCTION: tellEveryOne + public void tellEveryone(String message) + { + + Iterator it = clientOutputStreams.iterator(); + + while(it.hasNext()) + { + try + { + PrintWriter writer = (PrintWriter) it.next(); + writer.println(message); + writer.flush(); + } + catch(Exception ex) + { + ex.printStackTrace(); + } + } + } + + // FUNCTION: addAction + public void addAction() + { + JFrame window = new JFrame("SERVER"); + JPanel content = new JPanel(); + + clientsMessage = new JTextArea(15, 50); + clientsMessage.setLineWrap(true); + clientsMessage.setWrapStyleWord(true); + clientsMessage.setEditable(false); + + JScrollPane qScroller = new JScrollPane(clientsMessage); + qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); + + userList = new JTextArea(15, 10); + userList.setLineWrap(true); + userList.setWrapStyleWord(true); + userList.setEditable(false); + + JScrollPane uScroller = new JScrollPane(userList); + uScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + + smsToAll = new JTextField(20); + + sendButton = new JButton("Send To All"); + sendButton.addActionListener(new SendButtonListener()); + + content.add(qScroller); + content.add(uScroller); + content.add(smsToAll); + content.add(sendButton); + + window.getContentPane().add(BorderLayout.CENTER, content); + window.setSize(800, 400); + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.setVisible(true); + + clientsMessage.setText(""); + + } + + // FUNCTION: main + public static void main(String[] args) + { -public class ChatServer { - - JTextArea clientsMessage; - JTextArea userList; - ArrayList clientOutputStreams; - JButton sendButton; - JTextField smsToAll; - ArrayList uname = new ArrayList(); - ArrayList port = new ArrayList(); - - public class ClientHandler implements Runnable{ - - BufferedReader reader; - Socket sock; - //JTextArea clientsMessage; get nullPointerException for this line wasted one day - - public ClientHandler(Socket clientSocket) { - - try { - sock = clientSocket; - InputStreamReader isReader = new InputStreamReader(sock.getInputStream()); - reader = new BufferedReader(isReader); - - - }catch(Exception ex) { - ex.printStackTrace(); - } - } - - - public void run() { - String message; - String[] parts = null; - boolean once = false; - - - try { - while((message = reader.readLine()) != null) { - if(!once) - { - //System.out.println(sock.getPort()); - port.add(sock); - parts = message.split("\\:"); - //System.out.println(parts[0]); - uname.add(parts[0]); - once = true; - } - //System.out.println("read" + message); - clientsMessage.append(message+"\n"); - tellEveryone(message); - showingClients(); - } - uname.remove(parts[0]); - port.remove(sock); - showingClients(); - tellEveryone(parts[0].toUpperCase()+ "("+sock+")"+"has left the conversation\n"); - clientsMessage.append(parts[0].toUpperCase()+ "("+sock+")"+"has left the conversation\n"); - } - catch(Exception ex) { - ex.printStackTrace(); - } - - } - } - - public class SendButtonListener implements ActionListener{ - - @Override - public void actionPerformed(ActionEvent arg0) { - // TODO Auto-generated method stub - clientsMessage.append("SERVER : " + smsToAll.getText()+"\n"); - tellEveryone("SERVER : " + smsToAll.getText()); - - smsToAll.setText(""); - smsToAll.requestFocus(); - - } - - } - - - public void showingClients() - { - String st = "000..,"; - String t = ""; - for(String s : uname) - { - st += s+","; - t += s+"\n"; - } - - System.out.println(st); - - Iterator it = clientOutputStreams.iterator(); - userList.setText(t); - while(it.hasNext()) { - try { - PrintWriter writer = (PrintWriter) it.next(); - writer.println(st); - writer.flush(); - }catch(Exception ex) { - ex.printStackTrace(); - } - } - - - } - - public void go() { - clientOutputStreams = new ArrayList(); - - try { - ServerSocket serverSock = new ServerSocket(6666); - - while(true) { - Socket clientSocket = serverSock.accept(); - //System.out.println(clientSocket.getPort()); - PrintWriter writer = new PrintWriter(clientSocket.getOutputStream()); - clientOutputStreams.add(writer); - - - Thread t = new Thread(new ClientHandler(clientSocket)); - t.start(); - System.out.println("got a connection from "+clientSocket); - //showingClients(); - } - }catch(Exception ex) { - ex.printStackTrace(); - } - - } - - - - public void tellEveryone(String message) { - Iterator it = clientOutputStreams.iterator(); - while(it.hasNext()) { - try { - PrintWriter writer = (PrintWriter) it.next(); - writer.println(message); - writer.flush(); - }catch(Exception ex) { - ex.printStackTrace(); - } - } - } - public void addAction() - { - JFrame window = new JFrame("SERVER"); - JPanel content = new JPanel(); - clientsMessage = new JTextArea(15,50); - clientsMessage.setLineWrap(true); - clientsMessage.setWrapStyleWord(true); - clientsMessage.setEditable(false); - JScrollPane qScroller = new JScrollPane(clientsMessage); - qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); - qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); - - userList = new JTextArea(15, 10); - userList.setLineWrap(true); - userList.setWrapStyleWord(true); - userList.setEditable(false); - JScrollPane uScroller = new JScrollPane(userList); - uScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); - - - smsToAll = new JTextField(20); - sendButton = new JButton("Send To All"); - sendButton.addActionListener(new SendButtonListener()); - content.add(qScroller); - content.add(uScroller); - content.add(smsToAll); - content.add(sendButton); - window.getContentPane().add(BorderLayout.CENTER, content); - window.setSize(800, 400); - window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - window.setVisible(true); - clientsMessage.setText(""); - - } - - public static void main(String[] args) { - - ChatServer cs = new ChatServer(); - cs.addAction(); - cs.go(); - - - - } - - - + ChatServer cs = new ChatServer(); + + cs.addAction(); + cs.go(); + } } diff --git a/src/I.java b/src/I.java index 1da2aca..3ab6e7c 100755 --- a/src/I.java +++ b/src/I.java @@ -3,57 +3,80 @@ import java.io.*; import java.math.BigInteger; +// CLASS: Main class Main { + // FUNCTION: main public static void main (String[] args) throws java.lang.Exception { - // your code goes here + // your code goes here Scanner scan = new Scanner(System.in); int t = scan.nextInt(); - for(int cs=1;cs<=t;++cs){ - String a = scan.next(); - String b = scan.next(); - System.out.println(a+" "+b); - BigInteger bg = new BigInteger(a); - BigInteger gg = new BigInteger(b); - BigInteger ans = pow(bg,gg); - //System.out.println(ans.toString()); - String an = ans.toString(); - long gt = gv(an); - - while(gt >= 10) + + for(int cs = 1; cs <= t; ++cs) { - gt = sod(gt); - } - System.out.println("Case "+cs+": "+ gt); + String a = scan.next(); + String b = scan.next(); + + System.out.println(a + " " + b); + + BigInteger bg = new BigInteger(a); + BigInteger gg = new BigInteger(b); + BigInteger ans = pow(bg, gg); + //System.out.println(ans.toString()); + + String an = ans.toString(); + long gt = gv(an); + + while(gt >= 10) + { + gt = sod(gt); + } + System.out.println("Case " + cs + ": " + gt); } - + } + + // FUNCTION: sod public static long sod(long x) { long anss = 0; - while(x !=0) + + while(x != 0) { - anss += x%10; - x/=10; + anss += x % 10; + x /= 10; } + return anss; } - public static long gv(String x){ - long xx =0; - for(int i=0;i 0) { - if (exponent.testBit(0)) result = result.multiply(base); - base = base.multiply(base); - exponent = exponent.shiftRight(1); - } - return result; -} - + + // FUNCTION: pow + public static BigInteger pow(BigInteger base, BigInteger exponent) + { + BigInteger result = BigInteger.ONE; + + while (exponent.signum() > 0) + { + if (exponent.testBit(0)) result = result.multiply(base); + + base = base.multiply(base); + exponent = exponent.shiftRight(1); + } + return result; + } + } \ No newline at end of file