Skip to content

Commit

Permalink
Simple example of Java class: ComplexNumber with usage demo
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiled committed Feb 12, 2014
1 parent 46a7458 commit 7ee554b
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions seminars/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/class01/target/
35 changes: 35 additions & 0 deletions seminars/class01/nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<org-netbeans-modules-editor-indent.CodeStyle.usedProfile>project</org-netbeans-modules-editor-indent.CodeStyle.usedProfile>
<org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>4</org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>
<org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>4</org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>4</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>false</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>80</org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>none</org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaces-per-tab>4</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaces-per-tab>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaceBeforeMethodDeclParen>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaceBeforeMethodDeclParen>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaceBeforeMethodCallParen>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaceBeforeMethodCallParen>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.tab-size>4</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.tab-size>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.indent-shift-width>4</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.indent-shift-width>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.classDeclBracePlacement>NEW_LINE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.classDeclBracePlacement>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.expand-tabs>false</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.expand-tabs>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width>80</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.otherBracePlacement>NEW_LINE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.otherBracePlacement>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.methodDeclBracePlacement>NEW_LINE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.methodDeclBracePlacement>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-line-wrap>none</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-line-wrap>
</properties>
</project-shared-configuration>
25 changes: 25 additions & 0 deletions seminars/class01/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.mipt</groupId>
<artifactId>class01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>class01</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* ComplexNumber.java
* Created On Feb 26, 2013
* @author Andrei
*/
package ru.mipt.spring2014.class01;

public class ComplexNumber
{
private final double a, b;

public ComplexNumber (double a, double b)
{
this.a = a;
this.b = b;
}

//TODO: создать конструктор без аргументов

public double getReal ()
{
return a;
}

public double getIm ()
{
return b;
}

public ComplexNumber add (ComplexNumber other)
{
return new ComplexNumber (a + other.a, b + other.b);
}

public ComplexNumber subtract (ComplexNumber other)
{
//TOOD: реализовать по аналогии с методом add
throw new UnsupportedOperationException ("Division isn't supported yet");
}

//TODO: аналогичным образом реализовать методы умножения (multiply) и деления (divide)

@Override
public String toString ()
{
return String.valueOf (a) + " + " + b + "*i";
}

@Override
public boolean equals (Object obj)
{
if (obj instanceof ComplexNumber)
{
final ComplexNumber other = (ComplexNumber) obj;
return a == other.a && b == other.b;
} else
{
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Demo.java
* Created on Feb 13, 2014
*/
package ru.mipt.spring2014.class01;

public class Demo
{
public static void main (String[] args)
{
System.out.println ("ComplextNumber class demo");

ComplexNumber c1 = new ComplexNumber (5, 3), c2 = new ComplexNumber (2, -1);

System.out.println ("(" + c1 + ") + (" + c2 + ") = " + c1.add (c2));
}
}

0 comments on commit 7ee554b

Please sign in to comment.