176 lines
14 KiB
HTML
176 lines
14 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>MP3_CalebFontenot.java</title>
|
||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
|
<style type="text/css">
|
||
|
<!--
|
||
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
||
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
||
|
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
|
||
|
.literal {color: #cc7832}
|
||
|
.ST5 {font-family: monospace; font-weight: bold; font-style: italic}
|
||
|
.ST0 {color: #287bde}
|
||
|
.string {color: #6a8759}
|
||
|
.number {color: #6897bb}
|
||
|
.ST4 {color: #9876aa}
|
||
|
.ST6 {color: #ffc66d}
|
||
|
.ST1 {color: #8a653b}
|
||
|
.comment {color: #808080}
|
||
|
.whitespace {color: #505050}
|
||
|
.ST2 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
||
|
.ST3 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
||
|
-->
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/MP3_CalebFontenot/src/mp3_calebfontenot/MP3_CalebFontenot.java</td></tr></table>
|
||
|
<pre>
|
||
|
<span class="comment">/*</span>
|
||
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
||
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java</span><span class="comment"> to edit this template</span>
|
||
|
<span class="comment"> */</span>
|
||
|
<span class="literal">package</span> mp3_calebfontenot;
|
||
|
|
||
|
<span class="literal">import</span> java.util.Scanner;
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">class</span> MP3_CalebFontenot {
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST1">args</span> <span class="comment">the</span> <span class="comment">command</span> <span class="comment">line</span> <span class="comment">arguments</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args)
|
||
|
{
|
||
|
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST3">in</span>);
|
||
|
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Enter the first complex number: </span><span class="string">"</span>);
|
||
|
<span class="literal">double</span> a = input.nextDouble();
|
||
|
<span class="literal">double</span> b = input.nextDouble();
|
||
|
Complex c1 = <span class="literal">new</span> Complex(a, b);
|
||
|
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Enter the second complex number: </span><span class="string">"</span>);
|
||
|
<span class="literal">double</span> c = input.nextDouble();
|
||
|
<span class="literal">double</span> d = input.nextDouble();
|
||
|
Complex c2 = <span class="literal">new</span> Complex(c, d);
|
||
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">(</span><span class="string">"</span> + c1 + <span class="string">"</span><span class="string">)</span><span class="string">"</span> + <span class="string">"</span><span class="string"> + </span><span class="string">"</span> + <span class="string">"</span><span class="string">(</span><span class="string">"</span> + c2 + <span class="string">"</span><span class="string">)</span><span class="string">"</span> + <span class="string">"</span><span class="string"> = </span><span class="string">"</span>
|
||
|
+ c1.add(c2));
|
||
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">(</span><span class="string">"</span> + c1 + <span class="string">"</span><span class="string">)</span><span class="string">"</span> + <span class="string">"</span><span class="string"> - </span><span class="string">"</span> + <span class="string">"</span><span class="string">(</span><span class="string">"</span> + c2 + <span class="string">"</span><span class="string">)</span><span class="string">"</span> + <span class="string">"</span><span class="string"> = </span><span class="string">"</span>
|
||
|
+ c1.subtract(c2));
|
||
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">(</span><span class="string">"</span> + c1 + <span class="string">"</span><span class="string">)</span><span class="string">"</span> + <span class="string">"</span><span class="string"> * </span><span class="string">"</span> + <span class="string">"</span><span class="string">(</span><span class="string">"</span> + c2 + <span class="string">"</span><span class="string">)</span><span class="string">"</span> + <span class="string">"</span><span class="string"> = </span><span class="string">"</span>
|
||
|
+ c1.multiply(c2));
|
||
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">(</span><span class="string">"</span> + c1 + <span class="string">"</span><span class="string">)</span><span class="string">"</span> + <span class="string">"</span><span class="string"> / </span><span class="string">"</span> + <span class="string">"</span><span class="string">(</span><span class="string">"</span> + c2 + <span class="string">"</span><span class="string">)</span><span class="string">"</span> + <span class="string">"</span><span class="string"> = </span><span class="string">"</span>
|
||
|
+ c1.divide(c2));
|
||
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">|</span><span class="string">"</span> + c1 + <span class="string">"</span><span class="string">| = </span><span class="string">"</span> + c1.abs());
|
||
|
Complex c3 = (Complex) c1.clone();
|
||
|
System.<span class="ST3">out</span>.println(c1 == c3);
|
||
|
System.<span class="ST3">out</span>.println(c3.getRealPart());
|
||
|
System.<span class="ST3">out</span>.println(c3.getImaginaryPart());
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
<span class="literal">class</span> Complex {
|
||
|
|
||
|
<span class="literal">private</span> <span class="literal">double</span> <span class="ST4">a</span>;
|
||
|
<span class="literal">private</span> <span class="literal">double</span> <span class="ST4">b</span>;
|
||
|
<span class="literal">private</span> <span class="literal">final</span> <span class="literal">double</span> <span class="comment">i</span> = Math.<span class="ST5">sqrt</span>(-<span class="number">1</span>);
|
||
|
|
||
|
<span class="literal">public</span> Complex()
|
||
|
{
|
||
|
<span class="literal">this</span>.<span class="ST4">a</span> = <span class="number">0</span>;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> Complex(<span class="literal">double</span> a)
|
||
|
{
|
||
|
<span class="literal">this</span>.<span class="ST4">a</span> = a;
|
||
|
<span class="literal">this</span>.<span class="ST4">b</span> = <span class="number">0</span>;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> Complex(<span class="literal">double</span> a, <span class="literal">double</span> b)
|
||
|
{
|
||
|
<span class="literal">this</span>.<span class="ST4">a</span> = a;
|
||
|
<span class="literal">this</span>.<span class="ST4">b</span> = b;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> Complex <span class="ST6">add</span>(<span class="literal">double</span> c, <span class="literal">double</span> d)
|
||
|
{
|
||
|
<span class="literal">return</span> <span class="literal">new</span> Complex(<span class="literal">this</span>.<span class="ST4">a</span> + c, <span class="literal">this</span>.<span class="ST4">b</span> + d);
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> Complex <span class="ST6">add</span>(Complex obj)
|
||
|
{
|
||
|
<span class="literal">return</span> <span class="literal">new</span> Complex(<span class="literal">this</span>.<span class="ST4">a</span> + obj.<span class="ST4">a</span>, <span class="literal">this</span>.<span class="ST4">b</span> + obj.<span class="ST4">b</span>);
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> Complex <span class="ST6">subtract</span>(<span class="literal">double</span> c, <span class="literal">double</span> d)
|
||
|
{
|
||
|
<span class="literal">return</span> <span class="literal">new</span> Complex(<span class="literal">this</span>.<span class="ST4">a</span> - c, <span class="literal">this</span>.<span class="ST4">b</span> - d);
|
||
|
}
|
||
|
<span class="literal">public</span> Complex <span class="ST6">subtract</span>(Complex obj)
|
||
|
{
|
||
|
<span class="literal">return</span> <span class="literal">new</span> Complex(<span class="literal">this</span>.<span class="ST4">a</span> - obj.<span class="ST4">a</span>, <span class="literal">this</span>.<span class="ST4">b</span> - obj.<span class="ST4">b</span>);
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> Complex <span class="ST6">multiply</span>(<span class="literal">double</span> c, <span class="literal">double</span> d)
|
||
|
{
|
||
|
<span class="literal">double</span> r = <span class="literal">this</span>.<span class="ST4">a</span> * c - <span class="literal">this</span>.<span class="ST4">b</span> * d;
|
||
|
<span class="literal">double</span> i = <span class="literal">this</span>.<span class="ST4">a</span> * d + <span class="literal">this</span>.<span class="ST4">b</span> * c;
|
||
|
<span class="literal">return</span> <span class="literal">new</span> Complex(r, i);
|
||
|
}
|
||
|
<span class="literal">public</span> Complex <span class="ST6">multiply</span>(Complex obj) {
|
||
|
<span class="literal">double</span> r = <span class="literal">this</span>.<span class="ST4">a</span> * obj.<span class="ST4">a</span> - <span class="literal">this</span>.<span class="ST4">b</span> * obj.<span class="ST4">b</span>;
|
||
|
<span class="literal">double</span> i = <span class="literal">this</span>.<span class="ST4">a</span> * obj.<span class="ST4">b</span> + <span class="literal">this</span>.<span class="ST4">b</span> * obj.<span class="ST4">a</span>;
|
||
|
<span class="literal">return</span> <span class="literal">new</span> Complex(r, i);
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> Complex <span class="ST6">divide</span>(<span class="literal">double</span> c, <span class="literal">double</span> d)
|
||
|
{
|
||
|
<span class="literal">double</span> denominator = (c * c) + (<span class="literal">this</span>.<span class="ST4">b</span> * <span class="literal">this</span>.<span class="ST4">b</span>);
|
||
|
<span class="literal">double</span> aNum = (<span class="literal">this</span>.<span class="ST4">a</span> * c) + (<span class="literal">this</span>.<span class="ST4">b</span> * <span class="literal">this</span>.<span class="ST4">b</span>);
|
||
|
<span class="literal">double</span> iNum = (<span class="literal">this</span>.<span class="ST4">b</span> * c) - (<span class="literal">this</span>.<span class="ST4">a</span> * d);
|
||
|
<span class="literal">double</span> realResult = aNum / denominator;
|
||
|
<span class="literal">double</span> imaginaryResult = iNum / denominator;
|
||
|
<span class="literal">return</span> <span class="literal">new</span> Complex(realResult, imaginaryResult);
|
||
|
}
|
||
|
<span class="literal">public</span> Complex <span class="ST6">divide</span>(Complex obj)
|
||
|
{
|
||
|
<span class="literal">double</span> denominator = (obj.<span class="ST4">a</span> * obj.<span class="ST4">a</span>) + (<span class="literal">this</span>.<span class="ST4">b</span> * <span class="literal">this</span>.<span class="ST4">b</span>);
|
||
|
<span class="literal">double</span> aNum = (<span class="literal">this</span>.<span class="ST4">a</span> * obj.<span class="ST4">a</span>) + (<span class="literal">this</span>.<span class="ST4">b</span> * obj.<span class="ST4">b</span>);
|
||
|
<span class="literal">double</span> iNum = (<span class="literal">this</span>.<span class="ST4">b</span> * obj.<span class="ST4">a</span>) - (<span class="literal">this</span>.<span class="ST4">a</span> * obj.<span class="ST4">b</span>);
|
||
|
<span class="literal">double</span> realResult = aNum / denominator;
|
||
|
<span class="literal">double</span> imaginaryResult = iNum / denominator;
|
||
|
<span class="literal">return</span> <span class="literal">new</span> Complex(realResult, imaginaryResult);
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
<span class="literal">public</span> String <span class="ST6">toString</span>()
|
||
|
{
|
||
|
<span class="literal">return</span> <span class="ST4">a</span> + <span class="string">"</span><span class="string"> + </span><span class="string">"</span> + <span class="ST4">b</span> + <span class="string">"</span><span class="string">i</span><span class="string">"</span>;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> Complex <span class="ST6">clone</span>() {
|
||
|
Complex obj = <span class="literal">new</span> Complex(<span class="literal">t</span><span class="literal">his</span>.<span class="ST4">a</span>, <span class="literal">t</span><span class="literal">his</span>.<span class="ST4">b</span>);
|
||
|
<span class="literal">return</span> obj;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="literal">double</span> <span class="ST6">abs</span>() {
|
||
|
<span class="literal">return</span> Math.<span class="ST5">abs</span>(<span class="literal">this</span>.<span class="ST4">a</span> + <span class="literal">this</span>.<span class="ST4">b</span>);
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="literal">double</span> <span class="ST6">getRealPart</span>() {
|
||
|
<span class="literal">return</span> <span class="literal">this</span>.<span class="ST4">a</span>;
|
||
|
}
|
||
|
<span class="literal">public</span> <span class="literal">double</span> <span class="ST6">getImaginaryPart</span>() {
|
||
|
<span class="literal">return</span> <span class="literal">this</span>.<span class="ST4">b</span>;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
</pre></body>
|
||
|
</html>
|