Lösningsförslag till tentamen 121217



Similar documents
Homework/Program #5 Solutions

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

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

Chapter 2 Introduction to Java programming

Advanced Network Programming Lab using Java. Angelos Stavrou

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

Interface Programmera mot interface Johan Eliasson Johan Eliasson Interface kan bryta beroendekedjor Skriv generell kod «Type» Class2 Interface

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

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

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

Graphical User Interfaces

Introduction to Programming

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

AP Computer Science Static Methods, Strings, User Input

Using A Frame for Output

Programming with Java GUI components

Object-Oriented Programming in Java

Basics of Java Programming Input and the Scanner class

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Construction of classes with classes

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

Week 1: Review of Java Programming Basics

Using Files as Input/Output in Java 5.0 Applications

Java Appletek II. Applet GUI

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

More on Objects and Classes

CS 335 Lecture 06 Java Programming GUI and Swing

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

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

Course Intro Instructor Intro Java Intro, Continued

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Building Java Programs

public class demo1swing extends JFrame implements ActionListener{

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

Topic 11 Scanner object, conditional execution

Visit us at

Projektet Computer: Specifikation. Objektorienterad modellering och diskreta strukturer / design. Projektet Computer: Data. Projektet Computer: Test

D06 PROGRAMMING with JAVA

Chapter 3. Input and output. 3.1 The System class

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Using Two-Dimensional Arrays

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

CSE 8B Midterm Fall 2015

JAVA ARRAY EXAMPLE PDF

java Features Version April 19, 2013 by Thorsten Kracht

Tema: Encriptación por Transposición

Install Java Development Kit (JDK) 1.8

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

Sample CSE8A midterm Multiple Choice (circle one)

Building Java Programs

WRITING DATA TO A BINARY FILE

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

D06 PROGRAMMING with JAVA

Event-Driven Programming

11. Applets, normal window applications, packaging and sharing your work

Introduction to the Java Programming Language

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

CS 121 Intro to Programming:Java - Lecture 11 Announcements

AP Computer Science File Input with Scanner

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Remote Method Invocation

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

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

AP Computer Science Java Subset

JiST Graphical User Interface Event Viewer. Mark Fong

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

Chapter 2: Elements of Java

Skills and Topics for TeenCoder: Java Programming

Arrays in Java. Working with Arrays

CS 106 Introduction to Computer Science I

Line-based file processing

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

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

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

CS108, Stanford Handout #33. Sockets

Extending Desktop Applications to the Web

Comp 248 Introduction to Programming

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

Introduction to Java

6.1. Example: A Tip Calculator 6-1

CS 112/ Section 02 Hilal Ünal Aslıhan Ekim Merve Özkılınç Notes of March 11, 2008 and March 13, 2008: Pig Latin: add adday. main.

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

How to Convert an Application into an Applet.

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

Files and input/output streams

How Scala Improved Our Java

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

OBJECT ORIENTED PROGRAMMING IN JAVA EXERCISES

JAVA - FILES AND I/O

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

CS 1302 Ch 19, Binary I/O

ICOM 4015: Advanced Programming

Transcription:

Uppgift 1 Lösningsförslag till tentamen 121217 a) Utskriften blir: a = 5 och b = 10 a = 5 och b = 10 b) Utskriften blir 1 2 3 4 5 5 2 3 4 1 c) Utskriften blir 321 Uppgift 2 Med användning av dialogrutor: import java.util.*; public class QuadraticEquation { while(true) { String indata = JOptionPane.showInputDialog("Ange koeffienterna a, b och c: "); if (indata == null) break; Scanner sc = new Scanner(indata); double a = sc.nextdouble(); double b = sc.nextdouble(); double c = sc.nextdouble(); if (a == 0) JOptionPane.showMessageDialog(null, "Detta är ingen andragradsekvation!\n Försök igen!"); computeroots(a, b, c); //while //main public static void computeroots(double a, double b, double c) { double D = Math.pow(b, 2) / Math.pow(2*a, 2) - c / a; if (D == 0) { JOptionPane.showMessageDialog(null, "Har en dubbelrot i " + -b/(2 * a)); if (D > 0) { JOptionPane.showMessageDialog(null, "Rötterna är " + (-b / (2*a) + Math.sqrt(D)) + " och " + (-b / (2*a) - Math.sqrt(D)) ); { double real = -b / (2*a); double imaginary = Math.sqrt(Math.abs(D)); JOptionPane.showMessageDialog(null, "Imaginära rötter i " + real + " + " + imaginary + "i \n" + "och " + real + " - " + imaginary + "i."); // computeroots // QuadraticEquation

Med användning av System.in och System.out: import java.util.*; public class QuadraticEquation { while(true) { System.out.print("Ange koeffienterna a, b och c: "); Scanner sc = new Scanner(System.in); if (!sc.hasnext()) break; double a = sc.nextdouble(); double b = sc.nextdouble(); double c = sc.nextdouble(); if (a == 0) System.out.println("Detta är ingen andragradsekvation!\n Försök igen!"); computeroots(a, b, c); //while //main public static void computeroots(double a, double b, double c) { double D = Math.pow(b, 2) / Math.pow(2*a, 2) - c / a; if (D == 0) { System.out.println("Har en dubbelrot i " + -b/(2 * a)); if (D > 0) { System.out.println("Rötterna är " + (-b / (2*a) + Math.sqrt(D)) + " och " + (-b / (2*a) - Math.sqrt(D)) ); { double real = -b / (2*a); double imaginary = Math.sqrt(Math.abs(D)); System.out.println("Imaginära rötter i " + real + " + " + imaginary + "i \n" + "och " + real + " - " + imaginary + "i."); // computeroots // QuadraticEquation

Uppgift 3 public class FuelTank { private int capacity; private int fuellevel; public FuelTank(int capacity) { this.capacity = capacity; fuellevel = 0; //konstruktor public int getcapacity() { return capacity; //getcapacity public int getfuellevel() { return fuellevel; //getfuel public int fillfuel() { int fillingvolume = capacity - fuellevel; fuellevel = capacity; return fillingvolume; //fillfuel public void emptyfuel(int volume) { if ( fuellevel - volume >= 0) throw new IllegalArgumentException(); fuellevel = 0; //emptyfuel public int fillingrate() { return 100 *fuellevel / capacity; //fillingrate public String tostring() { return "Kapacitet: " + capacity + " liter\n" + "Återstående volym: " + fuellevel + " liter\n" + "Fyllnadsgrad: " + fillingrate() +"%"; //tostring //FuelTank Kommentar: Datatypen på instansvariablerna kan väljas till double. Detta medför då att inparameternas typer och returtyperna ändras till double för berörda metoder. Vidare förändras tostring metoden, eftersom värdena på instansvariablerna skrivs ut som heltal. Uppgift 4 import java.io.*; import java.util.scanner; public class Faulty { public static void main(string[] args) throws FileNotFoundException { String file = args[0]; Scanner in = new Scanner(new File(file)); int totally = 0; int faultybefore = 0; int faultyafter = 0; while (in.hasnextdouble() ) { double diff = in.nextdouble(); if (Math.abs(diff) >= 0.001) faultybefore++; if (Math.abs(diff) >= 0.0001) faultyafter++; in.close(); System.out.println("Ej accepterade axlar ökar från " + 100* (double) faultybefore/ totally + " % till " + 100 * (double) faultyafter/ totally + " %."); //main //Faulty

Uppgift 5 a) public int[] limitamplitud(int[] samples, int limit) { int[] newsamples = new int[samples.length]; for (int i = 0; i < samples.length; i = i + 1) { if (samples[i] > limit) newsamples[i] = limit; if (samples[i] < -limit) newsamples[i] = -limit; newsamples[i] = samples[i] return newsamples; // limitamplitud b) public static int[][][] rotate90(int[][][] picture) { int[][][] newpicture = new int[picture[0].length][picture.length][3]; for (int row = 0; row < picture.length; row++) { for (int col = 0; col < picture[row].length; col++) { for(int colors = 0; colors < picture[row][col].length; colors = colors + 1) newpicture[col][row][colors] = picture[row][picture[row].length - 1 - col][colors]; return newpicture; //rotate90

Uppgift 6 import java.awt.*; import java.awt.event.*; public class Team extends JPanel implements ActionListener { private JButton namebutton; private JLabel resultlabel; private int nrofgoals = 0; public Team (String teanname) { namebutton = new JButton(teanName); resultlabel = new JLabel("0", JLabel.CENTER); setlayout(new GridLayout(2, 1)); add(namebutton); add(resultlabel); namebutton.setbackground(color.yellow); resultlabel.setbackground(color.pink); namebutton.setbackground(color.pink); resultlabel.setbackground(color.yellow); resultlabel.setopaque(true); namebutton.addactionlistener(this); Font font = new Font("Times", Font.PLAIN, 26); namebutton.setfont(font); font = new Font("Times", Font.PLAIN, 36); resultlabel.setfont(font); //constructor public void actionperformed(actionevent e) { if (e.getsource() == namebutton) { nrofgoals = nrofgoals + 1; resultlabel.settext(" " + nrofgoals); //actionperformed //Team import java.awt.*; public class Match extends JFrame { public Match(String nameofhometeam, String nameofopponetteam) { getcontentpane().setlayout(new GridLayout(1, 2, 5, 5)); Team hometeam = new Team(nameOfHomeTeam); Team opponetteam = new Team(nameOfOpponetTeam); getcontentpane().add(hometeam); getcontentpane().add(opponetteam); pack(); setdefaultcloseoperation(exit_on_close); setvisible(true); //konstruktor Match v = new Match("Malmö FF", "IFK Göteborg"); //main //Match