...Calculator Program: import java.awt.*; import javax.swing.*; public class Calculator extends JFrame { JTextField display;// Display text field. JPanel buttonPanel;// Panel for buttons. JButton buttons[];// Array to create button objects. Calculator() { super("Calculator");// Gives the frame the title of Calculator. display = new JTextField();// Creates and stores the object of the text // field in the display. buttons = new JButton[16];// Array made up of 16 variables to signify // buttons. buttons[0] = new JButton("7");// Seven button. buttons[1] = new JButton("8");// Eight button. buttons[2] = new JButton("9");// Nine button. buttons[3] = new JButton("/");// Divide button. buttons[4] = new JButton("4");// Four button. buttons[5] = new JButton("5");// Five button. buttons[6] = new JButton("6");// Six button. buttons[7] = new JButton("*");// Multiply button. buttons[8] = new JButton("1");// One button. buttons[9] = new JButton("2");// Two button. buttons[10] = new JButton("3");// Three button. buttons[11] = new JButton("-");// Subtract button. buttons[12] = new JButton("0");// Zero button. buttons[13] = new JButton(".");// Dot button. buttons[14] = new JButton("=");// Equals button. buttons[15] = new JButton("+");// Add button. buttonPanel = new JPanel();// Creates panel for buttons. buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));// Sets the layout of // the...
Words: 917 - Pages: 4
...b=257; 4) boolean b=null; 5) int i=10; Question 2) What will happen if you try to compile and run the following code public class MyClass { public static void main(String arguments[]) { amethod(arguments); } public void amethod(String[] arguments) { System.out.println(arguments); System.out.println(arguments[1]); } } 1) error Can't make static reference to void amethod. 2) error method main not correct 3) error array must include parameter 4) amethod must be declared with String Question 3) Which of the following will compile without error 1) import java.awt.*; package Mypackage; class Myclass {} 2) package MyPackage; import java.awt.*; class MyClass{} 3) /*This is a comment */ package MyPackage; import java.awt.*; class MyClass{} Question 4) A byte can be of what size 1) -128 to 127 2) (-2 power 8 )-1 to 2 power 8 3) -255 to 256 4)depends on the particular implementation of the Java Virtual machine Question 5) What will be printed out if this code is run with the following command line? java myprog good morning public class myprog{ public static void main(String argv[]) { System.out.println(argv[2]); } } 1) myprog 2) good 3) morning 4) Exception raised: "java.lang.ArrayIndexOutOfBoundsException:...
Words: 1731 - Pages: 7
... |Answer |Must | |1 Phát biểu nào sau đây đối với gói java.awt là đúng? |Gói java.awt chứa các lớp hình thành |Sai | | |cơ sở cho việc thiết kế các chương | | | |trình | | | |Lớp java.awt chứa tất cả các lớp được |Sai | | |sử dụng cho các hoạt động vào và ra | | | |Lớp java.awt gồm các lớp được sử dụng |Đúng | | |để tạo ra các ứng dụng giao diện đồ | | | |họa (GUI) | | | |Lớp java.awt chứa các lớp hình thành |Sai | | |cơ sở...
Words: 4450 - Pages: 18
... 30); g2.draw(lin); } 3. How do you specify a particular color to be used as fill when using the Graphics class? g2.setPaint(Color.BLACK); 4. How would you create a SanSerif font of point size 14 that is bold and italic? Give an example. JButton b = new JButton("OK"); b.setFont(new Font("sansserif", Font.BOLD, 32)); 5. Given a graphics object g, write a few lines of code to have that graphics object draw a green circle (filled in) that has a diameter of 100 pixels. import java.awt.*; import java.applet.*; public class FirstApplet extends Applet { public void paint (Graphics g) { g.drawRect(40,20,200,100); g.drawOval(40,20,200,100); } 6. Given a graphics object g, write a few lines of code to draw a red rectangle (not filled in) with its upper left corner at position 25, 25 with a width of 100 and a height of 50. import java.awt.*; import java.applet.Applet; public class redRectangle extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawRect(25,25,100,50); } } 7. What is the paintComponent method? How is it used in...
Words: 625 - Pages: 3
...COMPUTER GRAPHICS Using Java CS 171G Outline • Terminology • Java Graphics Coordinate System • Java Applets • Drawing a Line • Changing the Color • Designing Your Own Colors • Drawing other shapes: rectangles, ovals, arcs, and polygons 1 Terminology: pixel • A pixel is the smallest visual element on a computer screen. Essentially, it’s a dot. • The image shown here is the letter S drawn in an area of 20 X 20 square pixels • The more pixels in an image, the smoother the image will appear. Terminology: resolution • Resolution: the number of pixels that can be displayed on a computer monitor. • When the resolution of a monitor is increased, pixels are smaller and closer together, so images appear smaller. • Typical computer monitor and HD television screen resolutions these days are • 720p: means 1366 pixels across by 768 pixels down • 900p: means 1600 pixels across by 900 pixels down • 1080p: means 1920 pixels across by 1080 pixels down All of the above are used in HDTVs and newer LCD displays (computer screens). 2 Resolution: Example of 900p 1600 pixels 900 pixels This screen has a total of 1,440,000 pixels Cartesian Coordinates • In math class, you learned about the Cartesian coordinate system. – Uses X (horizontal) and Y (vertical) axes – (0, 0) is at the center (also called the origin) – An increase in X means moving to the right – An increase in Y means moving up. – The Cartesian coordinate system has both positive and negative...
Words: 1930 - Pages: 8
...import java.awt.*; public class <ClassName> { public static void main (String[ ] args) { DrawingPanel <name> = new DrawingPanel (<width>, <height>); //to create a graphical window <panelname>.setBackground (Color.BLUE); Graphics g = <panel name>. getGraphics ( ); g.setColor (Color.BLUE); g. drawLine (<x1>, <y1>, <x2>, <y2>); //to draw a line from point (x1,y1) to (x2,y2) g.drawRect (<x>, <y>, <width>, <height>); //to draw a rectangle with the upper-left point (x,y) g.fillRect (<x>, <y>, <width>, <height>); //to fill a rectangle with color g.drawOval (<x>, <y>, <width>, <height>); //to draw the largest circle or oval that can fit into the rectangle with given width, height and the upper-left point (x,y) g.fillOval (<x>, <y>, <width>, <height>); //to fill a circle/oval with color Drawing panel keeps track of the overall image, but the actual drawing will be done with an object f type Graphics/ to draw shapes and lines, you don’t talk directly to the DrawingPanel, but rather to Graphisc所以说如果要“作画”的话,应该用g. 而不是 p./ 画画本来就是graphics的事,与panel无关 Rules of g.setColor: calling “setColor” is like dipping your paintbrush into a different color of paint. From that point on, all drawing and filling will be done in that specified color until set it to another color next time./ 先set颜色,再用该颜色去draw things...
Words: 293 - Pages: 2
...OBJECT ORIENTED PROGRAMMING IN JAVA ‐ EXERCISES CHAPTER 1 1. Write Text‐Based Application using Object‐Oriented Approach to display your name. // filename: Name.java // Class containing display() method, notice the class doesnt have a main() method public class Name { public void display() { System.out.println("Mohamed Faisal"); } } // filename: DisplayName.java // place in same folder as the Name.java file // Class containing the main() method public class DisplayName { public static void main(String[] args) { Name myname = new Name(); // creating a new object of Name class myname.display(); // executing the display() method in the Name class } } 2. Write a java Applet to display your age. // filename: DisplayNameApplet.java import java.applet.Applet; // import necessary libraries for an applet import java.awt.Graphics; public class DisplayNameApplet extends Applet { public void paint(Graphics g) { g.drawString("Mohamed Faisal", 50, 25); } } // filename: DisplayNameApplet.htm // place in same folder as the compiled DisplayNameApplet.class file Displaying my Name CHAPTER 2 3. Write a program that calculates and prints the product of three integers. // filename: Q1.java import java.util.Scanner; // import Scanner libraries for input public class Q1 { public static void main(String[] args) { Scanner input = new Scanner (System.in); int number1; int number2; int number3; System.out.println("Enter the First Number"); www.oumstudents...
Words: 3130 - Pages: 13
...toString() { String deadOrAlive = new String(); if (isAlive) { deadOrAlive = "A Living "; } else { deadOrAlive = "A Dead "; } return "[" + deadOrAlive + organismType + ": " + currentHP + "/" + maxHP + " | " + lastReproduced + "/" + reproduceEvery + " - ateThisTurn = " + ateThisTurn + "\n"; } public boolean justAte() { return ateThisTurn; } public boolean moveThisTurn() { return wantsToMove && isAlive; } public boolean isReadyToReproduce() { return super.isReadyToReproduce() && maxHP == currentHP && ateThisTurn && isAlive; } public boolean isBurning() { return false; } } import java.awt.*; import java.util.ArrayList; public class Breeder { public Breeder() { } public static void breed(Organism breedingOrganism, WorldWorker environment, Point pointHolder) { try { if (breedingOrganism.isReadyToReproduce()) { breedingOrganism.resetLastReproducedCounter(); boolean hadRoom = false; Organism babyOrganism = breedingOrganism.getClass().newInstance(); if (breedingOrganism instanceof Animal) { hadRoom =...
Words: 2671 - Pages: 11
...Java applets Slide 1 How applets work An applet is a special type of application that can be downloaded from an Internet or intranet server and run on the client’s computer within a web browser. The Java Plug-in is included as part of the JRE. It is a browser plug-in that allows a web browser to use a version of the JRE that’s newer than the one that’s included with the browser. For a client machine to run applets, a current version of the JRE and Java Plug-in must be installed on the client. If a user attempts to run an applet without first installing the current version of the Java Plug-in, a prompt will be displayed indicating that the Java Plug-in must be downloaded. Slide 2 Applet security issues To prevent applets from damaging a client system or from making it possible to damage a client system, security restrictions limit what an applet can do. To overcome these security restrictions, you can create a signed applet. This indicates that the applet comes from a trusted source. Then, you can add rights to the signed applet. Slide 3 What an applet can’t do Read, write, or delete files or databases on the client system. Access information about the files or databases on the client system. Run programs on the client system. Access system properties for the client system except the Java version, the name and version of the operating system, and the characters used to separate directories, paths, and lines. Make network connections to other servers available...
Words: 884 - Pages: 4
...class Untitled { public static void main(String[] args) { ** * Write a description of class PizzaApplet here. * * @author (zhencee) * @version (aversionnumberoradate) */ import java.awt.*; import java.awt.event.*; public class PizzaApp extends Frame { Frame f1 = new Frame(); Panel p1 = new Panel(); Panel p2 = new Panel(); Panel p3 = new Panel(); Panel p4 = new Panel(); Panel p5 = new Panel(); Panel p6 = new Panel(); Panel p7 = new Panel(); Label cashierName = new Label("Cashier Name:"); Label customerName = new Label("Customer Name"); Label Style = new Label("Style"); Label Option = new Label("Options"); Label Extras = new Label("Extras"); Label Size = new Label("Size"); Label Dine = new Label("Dine 0r Take Out"); Label Total = new Label("Total"); Label Change = new Label("Change"); Label cashTendered = new Label("Cash Tendered"); Label ricky = new Label("ZhensitivO_08@yahoo.com"); TextArea tArea1 = new TextArea("Transaction Registered"); Choice choice = new Choice(); Checkbox Cheese = new Checkbox("Cheese"); Checkbox Pepperoni = new Checkbox("Pepperoni"); Checkbox Mushroom = new Checkbox("Mushroom"); Checkbox Pineapple = new Checkbox("Pineapple"); Checkbox greenPepper = new Checkbox("Green Pepper"); TextField tf1CashierName = new TextField("zhencee"); TextField tf1CustomerName = new TextField(""); TextField tfTotal = new...
Words: 433 - Pages: 2
.../* * * Java II PRG 421 * June 20, 2011 * Week 4 Assignment * */ package javaapplication46; //import utilities import java.text.NumberFormat; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.awt.*; public class JavaApplication46 extends JFrame implements ActionListener { //Arrays declared int loanTermsYears[] = {7, 15, 30}; double annualRatesPercentage[] = {5.35, 5.5, 5.75}; public class JTabbedPane { JPanel mainSettings = new JPanel(); JTabbedPane tabs = new JTabbedPane(); } //Labels declared JLabel AmountLabel = new JLabel("Loan Amount:$ "); JLabel PaymentLabel = new JLabel("Monthly Payment: "); JLabel InterestLabel = new JLabel("Interest Rate %: "); JLabel TermLabel = new JLabel("Loan Term"); //Text Fields declared JTextField mortgageAmount = new JTextField(7); JTextField Payment = new JTextField(7); JTextField InterestRate = new JTextField(3); JTextField Term = new JTextField(3); //Buttons declared JButton Loan7yrs = new JButton("7 years @ 5.35%"); JButton Loan15yrs = new JButton("15 years @ 5.50%"); JButton Loan30yrs = new JButton("30 years @ 5.75%"); JButton ExitButton = new JButton("Exit"); JButton ClearButton = new JButton("Reset"); JButton CalculateButton = new JButton("Calculate"); // Text Area and ScrollPane declared JTextArea LoanPayments = new...
Words: 590 - Pages: 3
...import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.Random; class Hangmangame extends Applet implements ActionListener{ static final int DEAD=13; // amount of errors till loss private int errors; // amount of errors private String message; // error or victorie private String information; // information of the message private String rword; // the rword private StringBuffer gword;// the gword private Button bStart; // Button "Restart" private Button bGo; // Button "Go" private TextField tfLetter; // letter box private Font fnt; // common font public void init(){ fnt = new Font( "Monospaced", 0, 12 ); // Create textbox for guess letter tfLetter = new TextField(); // Create buttons and labels bStart = new Button("Restart"); bGo = new Button("Go"); // Add the graphical elements to the applet add(bStart); add(new Label("Guess a letter:")); add(tfLetter); add(bGo); // Buttons are events: bStart.addActionListener(this); bGo.addActionListener(this); // Start first game initGame(); ...
Words: 593 - Pages: 3
...import java.awt.*; import java.awt.event.*; import javax.swing.*; class Calculator extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; TextField txt=new TextField(15); JButton btn1 = new JButton("1"); JButton btn2 = new JButton("2"); JButton btn3 = new JButton("3"); JButton btn4 = new JButton("4"); JButton btn_arti = new JButton("+"); JButton btn5 = new JButton("5"); JButton btn6 = new JButton("6"); JButton btn7 = new JButton("7"); JButton btn8 = new JButton("8"); JButton btn_eksi = new JButton("-"); JButton btn9 = new JButton("9"); JButton btn0 = new JButton("0"); JButton btn_clr = new JButton("CLR"); JButton btn_carpi = new JButton("*"); JButton btn_bolu = new JButton("/"); JButton btn_esit = new JButton("="); String str_number = ""; int operation = 0; double int_number1 = 0; double int_number2 = 0; double result = 0; public Calculator() { JFrame frame = new JFrame("CALCULATOR"); frame.setSize(320,320); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setVisible(true); frame.setLayout(new BorderLayout()); JPanel HeadPanel = new JPanel(); JPanel NumberPanel = new JPanel(); JPanel LabelPanel = new JPanel(); LabelPanel.setBackground(Color.WHITE); HeadPanel.setBackground(Color.BLACK); NumberPanel.setLayout(new GridLayout(3,3)); LabelPanel.setLayout(new FlowLayout()); NumberPanel.add(btn1); btn1.addActionListener(this); ...
Words: 793 - Pages: 4
...import java.awt.*; import java.awt.event.*; import javax.swing.*; class Calculator extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; TextField txt=new TextField(15); JButton btn1 = new JButton("1"); JButton btn2 = new JButton("2"); JButton btn3 = new JButton("3"); JButton btn4 = new JButton("4"); JButton btn_arti = new JButton("+"); JButton btn5 = new JButton("5"); JButton btn6 = new JButton("6"); JButton btn7 = new JButton("7"); JButton btn8 = new JButton("8"); JButton btn_eksi = new JButton("-"); JButton btn9 = new JButton("9"); JButton btn0 = new JButton("0"); JButton btn_clr = new JButton("CLR"); JButton btn_carpi = new JButton("*"); JButton btn_bolu = new JButton("/"); JButton btn_esit = new JButton("="); String str_number = ""; int operation = 0; double int_number1 = 0; double int_number2 = 0; double result = 0; public Calculator() { JFrame frame = new JFrame("CALCULATOR"); frame.setSize(320,320); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setVisible(true); frame.setLayout(new BorderLayout()); JPanel HeadPanel = new JPanel(); JPanel NumberPanel = new JPanel(); JPanel LabelPanel = new JPanel(); LabelPanel.setBackground(Color.WHITE); HeadPanel.setBackground(Color.BLACK); NumberPanel.setLayout(new GridLayout(3,3)); LabelPanel.setLayout(new FlowLayout()); NumberPanel.add(btn1); btn1.addActionListener(this); ...
Words: 793 - Pages: 4
...LAB EXERCISE 1: READ FROM URL URLReader.java import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL yahoo = new URL("http://www.lelong.com/"); BufferedReader in = new BufferedReader( new InputStreamReader( yahoo.openStream())); String inputLine; for ( int i =0;(inputLine = in.readLine()) != null; i++){ System.out.println(inputLine);} in.close(); } } LAB EXERCISE 2: Echo Client EchoClient .java import java.io.*; import java.net.*; public class EchoClient { public static void main(String[] args) { try { Socket s = new Socket("127.0.0.1", 9999); BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter w = new PrintWriter(s.getOutputStream(), true); BufferedReader con = new BufferedReader(new InputStreamReader(System.in)); String line; do { line = r.readLine(); if ( line != null ) System.out.println(line); line = con.readLine(); w.println(line); } while ( !line.trim().equals("bye") ); } catch (Exception err) { System.err.println(err); } } } LAB EXERCISE 3:MULTI THREADS SimpleThreads.java public class SimpleThreads { //Display a message, preceded by the name of the current thread static void threadMessage(String message) { String threadName = Thread.currentThread().getName(); System...
Words: 1219 - Pages: 5