82 lines
4.9 KiB
HTML
82 lines
4.9 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>Bug2.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: Ubuntu}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: Ubuntu}
|
|
table {color: #888888; background-color: #313335; font-family: Ubuntu}
|
|
.literal {color: #cc7832}
|
|
.string {color: #6a8759}
|
|
.number {color: #6897bb}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST1 {color: #ffc66d; font-family: Ubuntu; font-style: italic}
|
|
.ST2 {color: #9876aa; font-family: Ubuntu; font-style: italic}
|
|
.ST0 {color: #808080; font-family: Ubuntu; font-weight: bold}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Assignments/lab13_CalebFontenot/src/lab13_calebfontenot/Bug2.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * To change this license header, choose License Headers in Project Properties.</span>
|
|
<span class="comment"> * To change this template file, choose Tools | Templates</span>
|
|
<span class="comment"> * and open the template in the editor.</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> lab13_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.util.Scanner;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">ASDV2</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> Bug2
|
|
{
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args)
|
|
{
|
|
Scanner in = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>);
|
|
<span class="literal">int</span> decimalNumber;
|
|
String binaryNumber;
|
|
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Enter a positive integer: </span><span class="string">"</span>);
|
|
decimalNumber = in.nextInt();
|
|
<span class="literal">int</span> x = <span class="number">0</span>;
|
|
<span class="literal">if</span> (decimalNumber <= <span class="number">0</span>)
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">ERROR: entered integer is nonpositive.</span><span class="string">"</span>);
|
|
<span class="literal">else</span> {
|
|
binaryNumber = <span class="string">""</span>;
|
|
<span class="comment">// algorithm step by step</span>
|
|
<span class="comment">// initial: binaryNumber = "", decimalNumber = 123</span>
|
|
<span class="comment">// step 1 : binaryNumber = "1 ", decimalNumber = 61</span>
|
|
<span class="comment">// step 2 : binaryNumber = "11 ", decimalNumber = 30</span>
|
|
<span class="comment">// step 3 : binaryNumber = "011 ", decimalNumber = 15</span>
|
|
<span class="comment">// step 4 : binaryNumber = "1011 ", decimalNumber = 7</span>
|
|
<span class="comment">// step 5 : binaryNumber = "1 1011 ", decimalNumber = 3</span>
|
|
<span class="comment">// step 6 : binaryNumber = "11 1011 ", decimalNumber = 1</span>
|
|
<span class="comment">// step 6 : binaryNumber = "111 1011 ", decimalNumber = 0</span>
|
|
<span class="comment">// stop : (decimalNumber != x)</span>
|
|
|
|
<span class="literal">while</span> (decimalNumber != x)
|
|
{
|
|
<span class="comment">//> add spaces to separate 4-digit groups</span>
|
|
<span class="literal">if</span> (binaryNumber.length() % <span class="number">5</span> == <span class="number">0</span>)
|
|
binaryNumber = <span class="string">"</span> <span class="string">"</span> + binaryNumber;
|
|
<span class="comment">//> extract last digit in binary representation</span>
|
|
<span class="comment">// and add it to binaryNumber</span>
|
|
binaryNumber = (decimalNumber % <span class="number">2</span>) + binaryNumber;
|
|
<span class="comment">//> cut last digit in binary representation</span>
|
|
decimalNumber /= <span class="number">2</span>;
|
|
}
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Binary: </span><span class="string">"</span> + binaryNumber);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|