Exp No: Date : Java Programming Page No: 10.write a program for sort of an array Program: import java.util.Scanner; class Sort { public static void main(String []args) { int n, c, d, swap; Scanner in = new Scanner(System.in); System.out.println("Input number of integers to sort"); n = in.nextInt(); int array[] = new int[n]; System.out.println("Enter " + n + " integers"); for (c = 0; c < n; c++) array[c] = in.nextInt(); for (c = 0; c < ( n - 1 ); c++) { for (d = 0; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For descending order use < */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } System.out.println("Sorted list of numbers"); } } for (c = 0; c < n; c++) System.out.println(array[c]); Output:sorted list: Given names aditya engg coll Names in Asending order aditya coll engg ADITYA ENGINEERING COLLEGE (CSE) 1 3 A 9 1 A 0 5 B 2 Exp No: Date : Java Programming Page No: 11)Write a java program that checks whether a given string is a palindrome or not. Program: import java.util.*; public class Palan { public static void main(String a[]) { System.out.println("Enter any string"); String s=new Scanner(System.in).next(); for(int i=0,j=s.length()-1;i<=j;i++,j--) { if(s.charAt(i)==s.charAt(j)); else { System.out.println("Not Palindrome"); System.exit(1); } } System.out.println("Palindrome"); } } Output: Enter any string: amma palindrome ADITYA ENGINEERING COLLEGE (CSE) 1 3 A 9 1 A 0 5 B 2 Java Programming Exp No: Date : Page No: AIM:. java program for user defined exception Program: class WrongInputException extends Exception { WrongInputException(String s) { super(s); } } class Input { void method() throws WrongInputException { throw new WrongInputException("Wrong input"); } } class TestInput { public static void main(String[] args){ try { new Input().method(); } catch(WrongInputException wie) { System.out.println(wie.getMessage()); } } } Output: Wrong exception. ADITYA ENGINEERING COLLEGE (CSE) 1 3 A 9 1 A 0 5 B 2 Exp No: Date : Java Programming Page No: AIM:.write a java program to create a class mytheard in this class constructor using super and starts the thread Program: class MyException extends Exception{ String str1; MyException(String str2) { str1=str2; } public String toString(){ return ("Output String = "+str1) ; } } class CustomException{ public static void main(String args[]){ try { throw new MyException("Custom"); // I'm throwing user defined custom exception above } catch(MyException exp){ System.out.println("Hi this is my catch block") ; System.out.println(exp) ; } } } Output: Hi this is my catch block Output String = Custom ADITYA ENGINEERING COLLEGE (CSE) 1 3 A 9 1 A 0 5 B 2 Java Programming Exp No: Date : Page No: AIM:.write a java program implementing the life cycle of an applet. a.Create a dialogbox and menu b.grid layout. import javax.swing.*; import java.awt.*; import java.awt.event.*; import com.bruceeckel.swing.*; public class TicTacToe extends JApplet { private JTextField rows = new JTextField("3"), cols = new JTextField("3"); private static final int BLANK = 0, XX = 1, OO = 2; class ToeDialog extends JDialog { private int turn = XX; // Start with x's turn ToeDialog(int cellsWide, int cellsHigh) { setTitle("The game itself"); Container cp = getContentPane(); cp.setLayout(new GridLayout(cellsWide, cellsHigh)); for(int i = 0; i < cellsWide * cellsHigh; i++) cp.add(new ToeButton()); setSize(cellsWide * 50, cellsHigh * 50); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } class ToeButton extends JPanel { private int state = BLANK; public ToeButton() { addMouseListener(new ML()); } public void paintComponent(Graphics g) { super.paintComponent(g); int ADITYA ENGINEERING COLLEGE (CSE) 1 3 A 9 1 A 0 5 B 2 Java Programming Exp No: Date : Page No: x1 = 0, y1 = 0, x2 = getSize().width - 1, y2 = getSize().height - 1; g.drawRect(x1, y1, x2, y2); x1 = x2/4; y1 = y2/4; int wide = x2/2, high = y2/2; if(state == XX) { g.drawLine(x1, y1, x1 + wide, y1 + high); g.drawLine(x1, y1 + high, x1 + wide, y1); } if(state == OO) g.drawOval(x1, y1, x1 + wide/2, y1 + high/2); } class ML extends MouseAdapter { public void mousePressed(MouseEvent e) { if(state == BLANK) { state = turn; turn = (turn == XX ? OO : XX); } else state = (state == XX ? OO : XX); repaint(); } } } } class BL implements ActionListener { public void actionPerformed(ActionEvent e) { JDialog d = new ToeDialog( ADITYA ENGINEERING COLLEGE (CSE) 1 3 A 9 1 A 0 5 B 2 Java Programming Exp No: Date : Page No: Integer.parseInt(rows.getText()), Integer.parseInt(cols.getText())); d.setVisible(true); } } public void init() { JPanel p = new JPanel(); p.setLayout(new GridLayout(2,2)); p.add(new JLabel("Rows", JLabel.CENTER)); p.add(rows); p.add(new JLabel("Columns", JLabel.CENTER)); p.add(cols); Container cp = getContentPane(); cp.add(p, BorderLayout.NORTH); JButton b = new JButton("go"); b.addActionListener(new BL()); cp.add(b, BorderLayout.SOUTH); } public static void main(String[] args) { Console.run(new TicTacToe(), 200, 100); } } ADITYA ENGINEERING COLLEGE (CSE) 1 3 A 9 1 A 0 5 B 2 Exp No: Date : Java Programming Page No: Ouput: ADITYA ENGINEERING COLLEGE (CSE) 1 3 A 9 1 A 0 5 B 2 Exp No: Date : Java Programming Page No: AIM:. write a java program for showing x and y position of the cursor movement using mouse Program: public partial class Form1 : Form { private LowLevelMouseProc _proc; private IntPtr _hookID = IntPtr.Zero; public Form1() { InitializeComponent(); InitHook(); } [System.Runtime.InteropServices.DllImport(user32.dll)] static extern bool SetCursorPos(int x, int y); [System.Runtime.InteropServices.DllImport(user32.dll)] public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); public const int MOUSEEVENTF_LEFTDOWN = 0x02; public const int MOUSEEVENTF_LEFTUP = 0x04; public void SimulateLeftMouseClick(int xpos, int ypos) { //I unhooked just to omit the issue off simulating the click mouse event UnhookWindowsHookEx(_hookID); var rect = pnlClientScreen.RectangleToScreen(pnlClientScreen.ClientRectangle); int thisTop = this.Top; float vertScaleRatio = (float)rect.Size.Height /(float)Screen.PrimaryScreen.Bounds.Height; float horzScaleRatio = (float)rect.Size.Width /(float)Screen.PrimaryScreen.Bounds.Width; int clientXpos = (int)( xpos*vertScaleRatio)rect.X; int clientYpos = (int)(ypos * horzScaleRatio)rect.Y; SetCursorPos(clientXpos, clientYpos); mouse_event(MOUSEEVENTF_LEFTDOWN, clientXpos,clientYpos, 0, 0); Thread.Sleep(100); mouse_event(MOUSEEVENTF_LEFTUP, clientXpos,clientYpos, 0, 0); } public void InitHook() { _proc = HookCallback; _hookID = SetHook(_proc); } private static IntPtr SetHook(LowLevelMouseProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return SetWindowsHookEx(WH_MOUSE_LL, proc, ADITYA ENGINEERING COLLEGE (CSE) 1 3 A 9 1 A 0 5 B 2 Exp No: Date : Java Programming Page No: GetModuleHandle(curModule.ModuleName), 0); } } private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam); private IntPtr HookCallback( int nCode, IntPtr wParam, IntPtr lParam) { if (nCode = 0 MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam) { MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); SimulateLeftMouseClick(hookStruct.pt.x, hookStruct.pt.y); //Console.WriteLine(hookStruct.pt.x , hookStruct.pt.y); } return CallNextHookEx(_hookID, nCode, wParam, lParam); } private const int WH_MOUSE_LL = 14; private enum MouseMessages { WM_LBUTTONDOWN = 0x0201, WM_LBUTTONUP = 0x0202, WM_MOUSEMOVE = 0x0200, WM_MOUSEWHEEL = 0x020A, WM_RBUTTONDOWN = 0x0204, WM_RBUTTONUP = 0x0205 } [StructLayout(LayoutKind.Sequential)] private struct POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] private struct MSLLHOOKSTRUCT { public POINT pt; public uint mouseData; public uint flags; public uint time; public IntPtr dwExtraInfo; } [DllImport(user32.dll, CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId); ADITYA ENGINEERING COLLEGE (CSE) 1 3 A 9 1 A 0 5 B 2 Exp No: Date : Java Programming Page No: [DllImport(user32.dll, CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport(user32.dll, CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport(kernel32.dll, CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName); private void pnlClientScreen_MouseClick(object sender, MouseEventArgs e) { Graphics gr = pnlClientScreen.CreateGraphics(); gr.DrawEllipse(Pens.Red, e.X-5,e.Y-5, 10, 10); gr.DrawEllipse(Pens.Red,e.X-7, e.Y-7, 14, 14); } private void button1_Click(object sender, EventArgs e) { InitHook(); } output: ADITYA ENGINEERING COLLEGE (CSE) 1 3 A 9 1 A 0 5 B 2 Exp No: Date : Java Programming ADITYA ENGINEERING COLLEGE (CSE) Page No: 1 3 A 9 1 A 0 5 B 2