473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

HW HELP! Input not being read?

The program asks for the user to enter a real number and an imaginary number followed by another set of a real number and an imaginary number. It then takes those and adds them, subtracts them, and multiplies them then displays it to the screen. However, after the user inputs the numbers, it does the operations, but it doesn't do the math right. For addition, only the second real number and second imaginary number inputted are scanned, so it adds the second real number to itself and the second imaginary number to itself instead of adding the first with the second like it's supposed to. The subtraction and multiplication operations don't scan any of the input at all though. I have one of the input used and output it produced at the very bottom commented out. If someone could tell me what I did wrong, I would greatly appreciate it, and thanks in advance!

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Complex
  5. {
  6.     /** declare variables */
  7.     public static double real;
  8.     public static double imaginary;
  9.  
  10.     /** default constructor */
  11.     public Complex()
  12.     {
  13.         real = 0.0;
  14.         imaginary = 0.0;
  15.     }
  16.  
  17.     /** assigns r to real and i to imaginary */
  18.     public Complex(double r, double i)
  19.     {
  20.         real = r;
  21.         imaginary = i;
  22.     }
  23.  
  24.     /** assigns r to real */
  25.     public void setReal(double r)
  26.     {
  27.         real = r;
  28.     }
  29.  
  30.     /** assigns i to imaginary */
  31.     public void setImaginary(double i)
  32.     {
  33.         imaginary = i;
  34.     }
  35.  
  36.     /** assigns real to r and returns it */
  37.     public double getReal()
  38.     {
  39.         double r = real;
  40.  
  41.         return real;
  42.     }
  43.  
  44.     /** assigns imaginary to i and returns it */
  45.     public double getImaginary()
  46.     {
  47.         double i = imaginary;
  48.  
  49.         return imaginary;
  50.     }
  51.  
  52.     /** declares and initializes realPart */
  53. //    public void setRealPart(double realPart)
  54. //    {
  55. //        double alpha;
  56. //        double realPart = realPart + alpha * i;
  57. //    }
  58.  
  59.     /** initializes object to alpha */
  60. //    public void setAlpha()
  61. //    {
  62. //        double alpha;
  63. //        Complex = alpha + alpha * i;
  64. //    }
  65.  
  66.     /** check for equality */
  67.     public Boolean equals(Complex C)
  68.     {
  69.         return((real == C.real) && (imaginary == C.imaginary));
  70.     }
  71.  
  72.     /** add */
  73.     public static Complex add(Complex C)
  74.     {
  75. /*        Complex add;
  76.         double r = real + C.real;
  77.         double i = imaginary + C.imaginary;
  78.         add = new Complex(r, i);
  79.  
  80.         return add; */
  81.  
  82.         return new Complex(real + C.real, imaginary + C.imaginary);
  83.     }
  84.  
  85.     /** subtract */
  86.     public static Complex subtract(Complex C)
  87.     {
  88. /*        Complex subtract;
  89.         double r = real - C.real;
  90.         double i = imaginary - C.imaginary;
  91.         subtract = new Complex(r, i);
  92.  
  93.         return subtract; */
  94.  
  95.         return new Complex(real - C.real, imaginary - C.imaginary);
  96.     }
  97.  
  98.     /** multiply */
  99.     public static Complex multiply(Complex C)
  100.     {
  101. /*        Complex multiply;
  102.         double r = real * C.real - imaginary * C.imaginary;
  103.         double i = real * C.imaginary + imaginary * C.real;
  104.         multiply = new Complex(r, i);
  105.  
  106.         return multiply; */
  107.  
  108.         return new Complex(real * C.real - imaginary * C.imaginary, real * C.imaginary + imaginary * C.real);
  109.     }
  110.  
  111.     /** displays to screen */
  112.     public String toString()
  113.     {
  114.         String imaginarySymbol = (imaginary < 0) ? " - " : " + ";
  115.  
  116.         return (real + imaginarySymbol + imaginary + "i");
  117.     }
  118.  
  119.     /** driver */
  120.     public static void main(String[] args)
  121.     {
  122.         Complex A = new Complex();
  123.         Complex B = new Complex();
  124.         Complex C = new Complex();
  125.  
  126.         Scanner input = new Scanner(System.in);
  127.  
  128.         /** input and scan first real number */
  129.         System.out.println("Enter a double for the first real number: ");
  130.         A.setReal(input.nextDouble());
  131.  
  132.         /** input and scan first imaginary number */
  133.         System.out.println("\nEnter a double for the first imaginary number: ");
  134.         A.setImaginary(input.nextDouble());
  135.  
  136.         /** input and scan second real number */
  137.         System.out.println("\nEnter a double for the second real number: ");
  138.         B.setReal(input.nextDouble());
  139.  
  140.         /** input and scan second imaginary number */
  141.         System.out.println("\nEnter a double for the second imaginary number: ");
  142.         B.setImaginary(input.nextDouble());
  143.  
  144.         /** add complex numbers and display to screen*/
  145.         A.add(B);
  146.         System.out.println(A.toString() + " + " + B.toString() + " = " + A.add(B));
  147.  
  148.         /** subtract complex numbers and display to screen*/
  149.         A.subtract(B);
  150.         System.out.println(A.toString() + " - " + B.toString() + " = " + A.subtract(B));
  151.  
  152.         /** multiply complex numbers and display to screen*/
  153.         A.multiply(B);
  154.         System.out.println(A.toString() + " * " + B.toString() + " = " + A.multiply(B));
  155.     }
  156. }
  157.  
  158.  
  159. /**
  160. Enter a double for the first real number:
  161. 1.0
  162.  
  163. Enter a double for the first imaginary number:
  164. 1.5
  165.  
  166. Enter a double for the second real number:
  167. 2.0
  168.  
  169. Enter a double for the second imaginary number:
  170. 2.5
  171. 4.0 + 5.0i + 4.0 + 5.0i = 8.0 + 10.0i
  172. 0.0 + 0.0i - 0.0 + 0.0i = 0.0 + 0.0i
  173. 0.0 + 0.0i * 0.0 + 0.0i = 0.0 + 0.0i
  174. Press any key to continue . . .
  175. */
  176.  
  177.  
Oct 6 '06 #1
1 1754
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Complex {
  4.  
  5.     /** declare variables */
  6.     double real;
  7.     double imaginary;
  8.  
  9.     /** default constructor */
  10.     public Complex() {
  11.         real = 0.0;
  12.         imaginary = 0.0;
  13.     }
  14.  
  15.     /** assigns r to real and i to imaginary */
  16.     public Complex(double r, double i) {
  17.         real = r;
  18.         imaginary = i;
  19.     }
  20.  
  21.     /** assigns r to real */
  22.     public void setReal(double r) {
  23.         real = r;
  24.     }
  25.  
  26.     /** assigns i to imaginary */
  27.     public void setImaginary(double i) {
  28.         imaginary = i;
  29.     }
  30.  
  31.     /** assigns real to r and returns it */
  32.     public double getReal() {
  33.         return real;
  34.     }
  35.  
  36.     /** assigns imaginary to i and returns it */
  37.     public double getImaginary() {
  38.         return imaginary;
  39.     }
  40.  
  41.     /** check for equality */
  42.     public boolean equals(Complex C) {
  43.         return((real == C.real) && (imaginary == C.imaginary));
  44.     }
  45.  
  46.     /** add */
  47.     public Complex add(Complex C) {
  48.         return new Complex(real + C.real, imaginary + C.imaginary);
  49.     }
  50.  
  51.     /** subtract */
  52.     public Complex subtract(Complex C) {
  53.         return new Complex(real - C.real, imaginary - C.imaginary);
  54.     }
  55.  
  56.     /** multiply */
  57.     public Complex multiply(Complex C) {
  58.         return new Complex(real * C.real - imaginary * C.imaginary, real * C.imaginary + imaginary * C.real);
  59.     }
  60.  
  61.     /** displays to screen */
  62.     public String toString() {
  63.         String imaginarySymbol = (imaginary < 0) ? "+" : " + ";
  64.  
  65.         return (real + " " + imaginarySymbol + " " + imaginary + "i");
  66.     }
  67.  
  68.     /** driver */
  69.     public static void main(String[] args) {
  70.         Complex A = new Complex();
  71.         Complex B = new Complex();
  72.         Complex C = new Complex();
  73.  
  74.         Scanner input = new Scanner(System.in);
  75.  
  76.         /** input and scan first real number */
  77.         System.out.println("Enter a double for the first real number: ");
  78.         A.setReal(input.nextDouble());
  79.  
  80.         /** input and scan first imaginary number */
  81.         System.out.println("\nEnter a double for the first imaginary number: ");
  82.         A.setImaginary(input.nextDouble());
  83.  
  84.         /** input and scan second real number */
  85.         System.out.println("\nEnter a double for the second real number: ");
  86.         B.setReal(input.nextDouble());
  87.  
  88.         /** input and scan second imaginary number */
  89.         System.out.println("\nEnter a double for the second imaginary number: ");
  90.         B.setImaginary(input.nextDouble());
  91.  
  92.         /** add complex numbers and display to screen*/
  93.         System.out.println(A.toString() + " + " + B.toString() + " = " + A.add(B).toString());
  94.  
  95.         /** subtract complex numbers and display to screen*/
  96.  
  97.         System.out.println(A.toString() + " - " + B.toString() + " = " + A.subtract(B).toString());
  98.  
  99.         /** multiply complex numbers and display to screen*/
  100.  
  101.         System.out.println(A.toString() + " * " + B.toString() + " = " + A.multiply(B).toString());
  102.     }
  103. }
you were not too far away
Oct 6 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.