Created
November 5, 2010 18:55
-
-
Save Cellane/664603 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ComplexNumber { | |
private float real, imaginary; | |
public ComplexNumber () { | |
this (0f, 0f); | |
} | |
public ComplexNumber (float real, float imaginary) { | |
this.real = real; | |
this.imaginary = imaginary; | |
} | |
public float getReal () { | |
return (real); | |
} | |
public void setReal (float real) { | |
this.real = real; | |
} | |
public float getImaginary () { | |
return (imaginary); | |
} | |
public void setImaginary (float imaginary) { | |
this.imaginary = imaginary; | |
} | |
public ComplexNumber multiply (ComplexNumber factor) { | |
ComplexNumber product = new ComplexNumber (); | |
product.setReal ((real * factor.real) - (imaginary * factor.imaginary)); | |
product.setImaginary ((imaginary * factor.real) + (real * factor.imaginary)); | |
return (product); | |
} | |
public ComplexNumber divide (ComplexNumber divisor) { | |
ComplexNumber quotient = new ComplexNumber (); | |
quotient.setReal (((real * divisor.real) + (imaginary * divisor.imaginary)) / | |
(float) (Math.pow (divisor.real, 2) + Math.pow (divisor.imaginary, 2))); | |
quotient.setImaginary (((imaginary * divisor.real) - (real * divisor.imaginary)) / | |
(float) (Math.pow (divisor.real, 2) + Math.pow (divisor.imaginary, 2))); | |
return (quotient); | |
} | |
public String toString () { | |
String string; | |
string = String.format ("%.2f + %.2fi", real, imaginary); | |
return (string); | |
} | |
public void print () { | |
System.out.println (toString ()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ComplexNumberTest extends junit.framework.TestCase { | |
ComplexNumber nullNumber1, nullNumber2; | |
ComplexNumber factor1, factor2, product; | |
ComplexNumber dividend, divisor, quotient; | |
public ComplexNumberTest () { | |
} | |
protected void setUp () { | |
System.out.println (String.format ("%s test started!", getName ())); | |
nullNumber1 = new ComplexNumber (); | |
factor1 = new ComplexNumber (13f, -3f); | |
factor2 = new ComplexNumber (6f, 2f); | |
dividend = new ComplexNumber (3f, 4f); | |
divisor = new ComplexNumber (2f, -5f); | |
} | |
protected void tearDown () { | |
System.out.println (String.format ("%s test finished!", getName ())); | |
} | |
public void testConstructor () { | |
nullNumber2 = new ComplexNumber (); | |
assertEquals ("Constructor without arguments fails (inequality)!", nullNumber1.toString (), | |
nullNumber2.toString ()); | |
assertNotNull ("Constructor without argument fails (null)!", nullNumber2); | |
} | |
public void testMultiplying () { | |
product = new ComplexNumber (); | |
product = factor1.multiply (factor2); | |
assertEquals ("Multiplying fails (real)!", product.getReal (), 84f); | |
assertEquals ("Multiplying fails (imaginary)!", product.getImaginary (), 8f); | |
assertEquals ("Multiplying fails (string)!", product.toString (), "84.00 + 8.00i"); | |
} | |
public void testDividing () { | |
quotient = new ComplexNumber (); | |
quotient = dividend.divide (divisor); | |
assertEquals ("Dividing fails (real)!", quotient.getReal (), -0.48, 0.01); | |
assertEquals ("Dividing fails (imaginary)!", quotient.getImaginary (), 0.79, 0.01); | |
assertEquals ("Dividing fails (string)!", quotient.toString (), "-0.48 + 0.79i"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Person { | |
private Scanner scanner = new Scanner (System.in); | |
protected Song[] songs; | |
protected String name; | |
protected int year; | |
public Person () { | |
this ("No name", 1990); | |
} | |
public Person (String name, int year) { | |
this.name = name; | |
this.year = year; | |
songs = setSongs (); | |
} | |
public Person (Person person) { | |
name = person.getName (); | |
year = person.getYear (); | |
songs = person.getSongs (); | |
} | |
public Song[] getSongs () { | |
return (songs); | |
} | |
protected Song[] setSongs () { | |
Song[] songs = new Song[readNumber ()]; | |
for (int i = 0; i < songs.length; i++) { | |
songs[i] = new Song (readString (i)); | |
} | |
return (songs); | |
} | |
public String getName () { | |
return (name); | |
} | |
public void setName (String name) { | |
this.name = name; | |
} | |
public int getYear () { | |
return (year); | |
} | |
public void setYear (int year) { | |
this.year = year; | |
} | |
private int readNumber () { | |
int i; | |
System.out.printf ("%-15s: ", "Number of songs"); | |
i = scanner.nextInt (); | |
return (i); | |
} | |
private String readString (int i) { | |
String string; | |
string = String.format ("%d. song", (i + 1)); | |
System.out.printf ("%-15s: ", string); | |
return (scanner.next ()); | |
} | |
public String songsToString () { | |
String string = ""; | |
for (int i = 0; i < songs.length; i++) { | |
string += songs[i].toString (); | |
if ((i + 1) != songs.length) { | |
string += ", "; | |
} else { | |
string += "."; | |
} | |
} | |
return (string); | |
} | |
public String toString () { | |
return (String.format ("%-15s: %s\n%-15s: %d\n%-15s: %s", "Name", getName (), | |
"Year of birth", getYear (), "Favourite songs", songsToString ())); | |
} | |
public void print () { | |
System.out.println (this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Point { | |
private int x, y; | |
public Point () { | |
this (0, 0); | |
} | |
public Point (int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
public Point (Point point) { | |
x = point.getX (); | |
y = point.getY (); | |
} | |
public int getX () { | |
return (x); | |
} | |
public void setX (int x) { | |
this.x = x; | |
} | |
public int getY () { | |
return (y); | |
} | |
public void setY (int y) { | |
this.y = y; | |
} | |
public void move (int dx, int dy) { | |
setX (getX () + dx); | |
setY (getY () + dy); | |
} | |
public String toString () { | |
return (String.format ("[%d; %d]", getX (), getY ())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Song { | |
private String name; | |
public Song (String name) { | |
this.name = name; | |
} | |
public String toString () { | |
return (name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Student extends Person { | |
private String field; | |
private int credits; | |
public Student () { | |
this ("No name", 1990, "None", 0); | |
} | |
public Student (String name, int year, String field, int credits) { | |
this.name = name; | |
this.year = year; | |
//songs = setSongs (); | |
this.field = field; | |
this.credits = credits; | |
} | |
public Student (Student student) { | |
name = student.getName (); | |
year = student.getYear (); | |
songs = student.getSongs (); | |
field = student.getField (); | |
credits = student.getCredits (); | |
} | |
public String getField () { | |
return (field); | |
} | |
public void setField (String field) { | |
this.field = field; | |
} | |
public int getCredits () { | |
return (credits); | |
} | |
public void setCredits () { | |
this.credits = credits; | |
} | |
public String toString () { | |
return (String.format ("%-15s: %s\n%-15s: %d\n%-15s: %s\n%-15s: %s\n%-15s: %d", | |
"Name", getName (), "Year of birth", getYear (), "Favourite songs", songsToString (), | |
"Field", getField (), "Credits", getCredits ())); | |
} | |
public void print () { | |
System.out.println (this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StudentTest { | |
public static void main (String args[]) { | |
Student student1, student2; | |
student1 = new Student (); | |
student2 = new Student ("Sue", 1980, "Informatics", 60); | |
student1.print (); | |
student2.print (); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Triangle { | |
private Point a, b, c; | |
public Triangle () { | |
this (new Point (), new Point (), new Point ()); | |
} | |
public Triangle (Point a, Point b, Point c) { | |
this.a = new Point (a); | |
this.b = new Point (b); | |
this.c = new Point (c); | |
} | |
public Triangle (Triangle triangle) { | |
a = new Point (triangle.getA ()); | |
b = new Point (triangle.getB ()); | |
c = new Point (triangle.getC ()); | |
} | |
public Point getA () { | |
return (a); | |
} | |
public Point getB () { | |
return (b); | |
} | |
public Point getC () { | |
return (c); | |
} | |
public void move (int dx, int dy) { | |
a.move (dx, dy); | |
b.move (dx, dy); | |
c.move (dx, dy); | |
} | |
public String toString () { | |
return (String.format ("A: %s, B: %s, C: %s", a, b, c)); | |
} | |
public void print () { | |
System.out.println (this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TriangleTest { | |
public static void main (String args[]) { | |
Triangle triangle1, triangle2; | |
triangle1 = new Triangle (); | |
triangle2 = new Triangle (new Point (20, 20), new Point (30, 30), new Point (10, 30)); | |
triangle1.print (); | |
triangle1.move (10, 10); | |
triangle1.print (); | |
triangle2.print (); | |
triangle2.move (-10, -10); | |
triangle2.print (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment