HBV201G Viðmótsforritun

Similar documents
INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

CS 335 Lecture 06 Java Programming GUI and Swing

DHBW Karlsruhe, Vorlesung Programmieren, Remote Musterlösungen

Graphical User Interfaces

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

Advanced Network Programming Lab using Java. Angelos Stavrou

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

Homework/Program #5 Solutions

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

public class demo1swing extends JFrame implements ActionListener{

Using NetBeans IDE for Desktop Development. Geertjan Wielenga

Assignment # 2: Design Patterns and GUIs

Informatik II. // ActionListener hinzufügen btnconvert.addactionlistener(this); super.setdefaultcloseoperation(jframe.

Programming with Java GUI components

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

How to Convert an Application into an Applet.

Java Appletek II. Applet GUI

Essentials of the Java(TM) Programming Language, Part 1

Using A Frame for Output


Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Swing. A Quick Tutorial on Programming Swing Applications

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Extending Desktop Applications to the Web

Essentials of the Java Programming Language

Chapter 2 Introduction to Java programming

Dev Articles 05/25/07 11:07:33

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

Karsten Lentzsch JGoodies SWING WITH STYLE

JIDE Action Framework Developer Guide

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

Konzepte objektorientierter Programmierung

ICOM 4015: Advanced Programming

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

How Scala Improved Our Java

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

Not agree with bug 3, precision actually was. 8,5 not set in the code. Not agree with bug 3, precision actually was

The Snake Game Java Case Study

Lab 9. Spam, Spam, Spam. Handout 11 CSCI 134: Spring, To gain experience with Strings. Objective

if and if-else: Part 1

Collections.sort(population); // Método de ordenamiento

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Java Program Coding Standards Programming for Information Technology

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Nexawebホワイトペーパー. Developing with Nexaweb ~ Nexaweb to Improve Development Productivity and Maintainability

AP Computer Science Static Methods, Strings, User Input

Topic 11 Scanner object, conditional execution

Introduction to the Java Programming Language

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

J a v a Quiz (Unit 3, Test 0 Practice)

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Building Java Programs

Example of a Java program

The following program is aiming to extract from a simple text file an analysis of the content such as:

JiST Graphical User Interface Event Viewer. Mark Fong

1 Hour, Closed Notes, Browser open to Java API docs is OK

Part I. Multiple Choice Questions (2 points each):

Creating a Simple, Multithreaded Chat System with Java

How To Write A Program For The Web In Java (Java)

Lösningsförslag till tentamen

Altas, Bajas y Modificaciones de Registros en tabla MYSQL

Tutorial Reference Manual. Java WireFusion 4.1

Fachbereich Informatik und Elektrotechnik Java Swing. Advanced Java. Java Swing Programming. Programming in Java, Helmut Dispert

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Tema: Encriptación por Transposición

Final Project Report E3390 Electronic Circuit Design Lab. Electronic Notepad

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Java SE 6 Update 10. la piattaforma Java per le RIA. Corrado De Bari. Sun Microsystems Italia Spa. Software & Java Ambassador

Realizing Enterprise Integration Patterns in WebSphere

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Introduction to Object-Oriented Programming

Java is commonly used for deploying applications across a network. Compiled Java code

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

Java GUI Programming

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Tutorial: Time Of Day Part 2 GUI Design in NetBeans

AP Computer Science Java Mr. Clausen Program 9A, 9B

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

Introduction to Java

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Construction of classes with classes

WRITING DATA TO A BINARY FILE

Skills and Topics for TeenCoder: Java Programming

public class Application extends JFrame implements ChangeListener, WindowListener, MouseListener, MouseMotionListener {

How to Install Java onto your system

Programming to Interfaces

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Object Oriented Programming with Java. School of Computer Science University of KwaZulu-Natal

Summer Internship 2013

Mouse Event Handling (cont.)

Transcription:

HBV201G Viðmótsforritun Forritunarverkefni 4 Ágiskunarleikur eða RPN reiknivél (Java Swing) 5. mars 2012 Nemandi: Dæmakennari: Sveinn Flóki Guðmundsson Elvar Helgason 1 of 5

GuessGame.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.random; How to use: GuessGame mygame = new GuessGame(); After: mygame is a new GuessGame, with int secret_number random from 0 to 1000, int num_of_tries = 0, with instructions and suitable feedback, and Boolean you_won set to false. public class GuessGame Function: sets the location of JFrame frame to the center of the screen static void centerwindow(jframe frame) Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); int x = (int) ((dimension.getwidth() - frame.getwidth()) / 2); int y = (int) ((dimension.getheight() - frame.getheight()) / 2); frame.setlocation(x, y); Function: returns true if String s contains an integer, else returns false static boolean str_has_int(string s) int a; try a = Integer.parseInt(s); catch(numberformatexception e) return false; return true; private JTextField entryguess; private JButton buttonguess, buttonre, buttonexit; private JLabel labelinfo, labelhint; int num_of_tries = 0; int secret_number; Boolean you_won = false; public GuessGame() JFrame frame = new JFrame("Simple Guess Game"); frame.setdefaultcloseoperation(jframe.exit_on_close); entryguess = new JTextField(10); buttonguess = new JButton("Guess"); buttonre = new JButton("Restart"); buttonexit = new JButton("Exit"); labelinfo = new JLabel("<html><div style=\"width: 308px; height: 40px; text-align: center;\"><p>this is a game where you guess a number between 0 and 1000. To guess a number write it in the entry below and press \"Guess\". If you're smart you shouldn't have to guess more than 11 times.</p></div></html>"); labelhint = new JLabel("<html><div style=\"width: 310px; height: 40px; text-align: center;\"><p>you haven't guessed a number yet.</div></html>"); entryguess.addactionlistener(new OnGuess()); buttonguess.addactionlistener(new OnGuess()); buttonre.addactionlistener(new OnRefresh()); buttonexit.addactionlistener(new QuitGuessGame()); Container framecontainer = frame.getcontentpane(); JPanel panel1 = new JPanel(); 2 of 5

framecontainer.add(labelinfo, BorderLayout.PAGE_START); panel1.add(entryguess); panel1.add(buttonguess); panel1.add(buttonre); panel1.add(buttonexit); framecontainer.add(panel1, BorderLayout.CENTER); framecontainer.add(labelhint, BorderLayout.PAGE_END); Random g = new Random(); secret_number = g.nextint(1000); System.out.println("Secret number: " + secret_number); frame.setsize(200,200); frame.pack(); centerwindow(frame); frame.setvisible(true); Before: int secret_number between 0 and 1000, int num_of_tries=>0, JTextField entryguess set, JLabel labelhint set, and Boolean you_won set to false or true. After: If you_won is true, empty entryguess and do nothing more. Try to turn string from entryguess to int, if it fails return suitable string to labelhint and do nothing more. Raise num_of_tries by 1. Compare secret_number and n, return suitable string to labelhint, if secret_number == n, set you_won to true. private class OnGuess extends AbstractAction String s = entryguess.gettext(); entryguess.settext(""); if(you_won) return; String string_guesses = ""; String string_hint = ""; String how_great = ""; if(str_has_int(s)) int n = Integer.parseInt(s); num_of_tries++; if (num_of_tries == 1) string_guesses = "You've guessed once. "; else string_guesses = "You've guessed " + num_of_tries + " times. "; cheat?"; Master Of Orion. Congratulations!"; you can't do better?"; if (secret_number == n) if (num_of_tries == 1) how_great = "WTF? You sure you didn't else if (num_of_tries < 6) how_great = "You clearly are the true else if (num_of_tries < 12) how_great = "Well done, but are you sure else if (num_of_tries > 11) how_great = "Meh! You won't impress 3 of 5

anyone with a score like that!"; " + how_great + ""; Try again!"; Try again!"; string_hint = "The number " + n + " is correct. you_won = true; else if (secret_number > n) string_hint = "The number " + n + " is too low. else if (secret_number < n) string_hint = "The number " + n + " is too high. else string_hint = "You must write a number in the entry!"; labelhint.settext("<html><div style=\"width: 310px; height: 40px; text-align: center;\"><p>"+string_guesses+string_hint+"</p></p></div></html>"); Before: int secret_number=>0, int num_of_tries=>0, JLabel labelhint set, and Boolean you_won set to false or true. After: Set secret_number to a new random number from 0 to 1000. Set you_won to false. Set num_of_tries to 0. Return suitable string to labelhint. private class OnRefresh extends AbstractAction Random r = new Random(); secret_number = r.nextint(1000); num_of_tries = 0; you_won = false; labelhint.settext("<html><div style=\"width: 310px; height: 40px; text-align: center;\"><p>new Game. You haven't guessed a number yet.</p></p></div></html>"); System.out.println("New game."); System.out.println("Secret number: " + secret_number); After: Quit the application. (rather crude operation but effective) private class QuitGuessGame extends AbstractAction System.exit(0); public static void main(string[] args) new GuessGame(); 4 of 5

Hægt er að nálgast skrána GuessGame.java hér: https:notendur.hi.is/sfg6/vidmotsforritun/forritunarverkefni4.zip 5 of 5