Övning 4. Repetition göra egna klasser class Rektangel { private double längd; private double bredd; public Rektangel(double l, double b) { this.längd.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Lecture 4 More on Java® Data Types, Control Structures.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Methods and Formatted Output Chapter 4. Overview Breaking down a big program into small methods. Formatting output – using printf – using DecimalFormat.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright © Recursive GCD Demo public class.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Programming Methodology (1). Implementing Methods main.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Phil Campbell London South Bank University Java 1 First Steps.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
TNPL JoongJin-Cho Runge-kutta’s method of order 4.
1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Method main Point p1 = new Point(5, 6); Point p2 = new Point(10, 11); System.out.println(p1.getX()); System.out.println( Point.num );
Hand Trace and Output for: int digit = 0; int number = 1423; do { digit = number % 10; System.out.println(digit); number = number / 10; } while (number.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Değişik Parametreli Nesne Tanımı 1. Object with parameter Class Box { double width; double height; double depth; Box(Box ob) { width=ob.width; height=ob.height;
Using Classes to Store Data Computer Science 2 Gerb.
Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Övning 5. Repetition klasser class Rektangel { private static int antal = 0; private double längd; private double bredd; public Rektangel(double l, double.
FEN KbP: Seminar 2/JML-Intro1 JML Introduktion Specifikation af en Personklasse.
Paradigmer i Programmering 4 a. Java 1.5 -Generics -Autoboxing/unboxing -New for sætning -Static imports -enum.
ISQA 360 – July 8, 2002 Methods Dr. Sergio Davalos.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Import javax.swing.JOptionPane; public class Rectangle { public static void main(String[] args) { double width, length, area, perimeter; String lengthStr,
Staples are our staple Building upon our solution.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Chapter 2 Clarifications
Examples of Classes & Objects
AKA the birth, life, and death of variables.
using System; namespace Demo01 { class Program
Sum of natural numbers class SumOfNaturalNumbers {
Function Call Trace public class Newton {
Functions Used to write code only once Can use parameters.
null, true, and false are also reserved.
TO COMPLETE THE FOLLOWING:
Classes & Objects: Examples
Java Lesson 36 Mr. Kalmes.
Code Animation Examples
Recursive GCD Demo public class Euclid {
AKA the birth, life, and death of variables.
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Web Service.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Object-Oriented Programming and class Design
Object-Oriented Programming and class Design
Presentation transcript:

Övning 4

Repetition göra egna klasser

class Rektangel { private double längd; private double bredd; public Rektangel(double l, double b) { this.längd = l; this.bredd = b; } public void info() { System.out.println(”Längd:”); System.out.println(this.längd); System.out.println(”Bredd:”); System.out.println(this.bredd); } Längd: 2.0 Bredd: 3.0 main class RektangelEx2 { public static void main(String [] arg) { Rektangel r; r = new Rektangel(2.0, 3.0); r.info(); } InstanserKlassvariablerMetodvariabler main Rektangel RektangelEx2 Rektangel null r arg 3.0 b 2.0 l 0.0 bredd 0.0 längd this info this

class Rektangel { private double längd; private double bredd; public Rektangel(double l, double b) { längd = l; bredd = b; } public void info() { System.out.println(”Längd ” + längd); System.out.println(”Bredd ” + bredd); } Längd 2.0 Bredd 3.0 main class RektangelEx3 { public static void main(String [] arg) { Rektangel r; r = new Rektangel(2.0, 3.0); r.info(); } InstanserKlassvariablerMetodvariabler main Rektangel RektangelEx3 Rektangel null r arg 3.0 b 2.0 l 0.0 bredd 0.0 längd this info this

forts… göra egna klasser

class Rektangel { public static int antal = 0; private double längd; private double bredd; public Rektangel(double l, double b) { längd = l; bredd = b; antal++; } public void info() { System.out.println(”Längd ” + längd); System.out.println(”Bredd ” + bredd); } Antal: 0 Antal: 2 Längd 2.0 Bredd 3.0 Längd 4.5 Bredd 2.0 main class RektangelEx6 { public static void main(String [] arg) { System.out.println(”Antal: ” + Rektangel.antal); Rektangel r1 = new Rektangel(2.0,3.0); Rektangel r2 = new Rektangel(4.5,2.0); System.out.println(”Antal: ” + Rektangel.antal); r1.info(); r2.info(); } InstanserKlassvariablerMetodvariabler main Rektangel RektangelEx6 Rektangel r1 null arg 3.0 b 2.0 l 0.0 bredd 0.0 längd this 0.0 bredd 0.0 längd this 4.5 l 2.0 b r2 Rektangel 0 antal 12

class Rektangel { private static int antal = 0; private double längd; private double bredd; public Rektangel(double l, double b) { längd = l; bredd = b; antal++; } public void info() { System.out.println(”Längd ” + längd); System.out.println(”Bredd ” + bredd); } public static int antalSkapadeRek() { return antal; } Antal: 0 Antal: 2 Längd 2.0 Bredd 3.0 Längd 4.5 Bredd 2.0 main class RektangelEx7 { public static void main(String [] arg) { System.out.println(”Antal: ” + Rektangel.antalSkapadeRek() ); Rektangel r1 = new Rektangel(2.0,3.0); Rektangel r2 = new Rektangel(4.5,2.0); System.out.println(”Antal: ” + Rektangel.antalSkapadeRek() ); r1.info(); r2.info(); } InstanserKlassvariablerMetodvariabler main Rektangel RektangelEx7 r1 null arg 0.0 bredd 0.0 längd bredd 0.0 längd r2 0 antal 12 antalSkapadeRek

class Rektangel { private static int antal = 0; private double längd; private double bredd; public Rektangel(double l, double b) { längd = l; bredd = b; antal++; } public double area() { return längd * bredd; } public static double area(double l, double b) { return l * b; } area: 6.0 area: 12.0 area: 4.0 main class RektangelEx8 { public static void main(String [] arg) { Rektangel r = new Rektangel(2.0, 3.0); double area; area = r.area(); System.out.println(”area: ” + area); area = Rektangel.area(3.0, 4.0); System.out.println(”area: ” + area); area = r.area(2.0, 2.0); System.out.println(”area: ” + area); } InstanserKlassvariablerMetodvariabler main Rektangel RektangelEx7 r null arg 0.0 bredd 0.0 längd antal 1 area 0.0 this area area l 3.0 b 4.0 area l 2.0 b 4.0