The Artima Developer Community
Sponsored Link

C# Answers Forum
new to java help

1 reply on 1 page. Most recent reply: Nov 7, 2002 5:23 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic    
Flat View: This topic has 1 reply on 1 page
salem

Posts: 2
Nickname: salemjr
Registered: Oct, 2002

new to java help Posted: Oct 10, 2002 5:49 PM
Reply to this message Reply
Advertisement
hi
im trying to write a java program that produce results like this
c:\> java Calc

*
8
9

72

%
5
8

5

\
99
8

Unknown operation: \
Enter 'e' to quit.

/
99
8

12

e

c:\>

------
NOW I WrOTE THIS PROGRAM BUT I CANT TERMINATE THE LOOP PLS CAN SOME ONE HELP
THANX

HERE IS THE PROGRAM


code:
import printer.*; //this is java package i am using
public class Calc
{
public static void main (String[] args)
{
boolean done = false;
char operand;

while(!done)
{
System.out.println("Please enter the operation you wish to perform:");
System.out.println(" (+) for Addition");
System.out.println(" (-) for Subtraction");
System.out.println(" (*) for Multiplication");
System.out.println(" (/) for Division");

operand = printer.readChar();
printer.println("-");
int a = printer.println.readInt();
printer.println("-");
int b = printer.readInt();
printer.println("- \n");

if (operand=='*')
{
printer.println(a*b);

else if (operand=='+')
{
printer.println(a+b);
}
else if (operand=='e')
{
//some cose to exit without errors
}
else
{
printer.println("Invalid input!");
printer.println("Enter 'e' to quit.");
}
}
}
i need to exit from the loop without errors im trying but i have no luck, i need to enter e in order to end the program.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: new to java help Posted: Nov 7, 2002 5:23 PM
Reply to this message Reply
How's this? I made a few improvements over what you requested:
You can type in the values and operator in any old order.
It has a graphical interface instead of console interface.
The calculator class works a little like the old HP calculators, using the reverse Polish notation approach.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
 
namespace WindowsApplication2
{
	class Calculator
	{
		private Stack stack = new Stack();
 
		public void Push( double a )
		{
			stack.Push( a );
		}
		
		public double Pop()
		{
			return (double)stack.Pop();
		}
 
		/// Todo: make this a property called "Value" or "Result"...
		public double Peek()
		{
			return (double)stack.Peek();
		}
 
		public double Add()
		{
			Push( Pop() + Pop() );			
			return Peek();
		}
		
		public double Sub()
		{
			// The one at the top is subtracted from the next one down.
			// (Pop() - Pop()) would get the wrong result.
			Push( - Pop() + Pop() );  
			return Peek();
		}
		
		public double Mult()
		{
			Push( Pop() * Pop() );			
			return Peek();
		}
		
		public double Div()
		{
			double divisor = Pop();
			Push( Pop() / divisor );			
			return Peek();
		}
 
		public double Mod()
		{
			// Not relevant for floating point, so convert to int:
			int divisor = (int)Pop();
		
			Push( (int)Pop() % divisor );
			return Peek();
		}
 
		bool IsNum( string s )
		{
			// try converting it...
			try
			{
				float.Parse(s);
				return true;
			}
			catch( Exception e )
			{
				return false;
			}
		}
 
		public double Calc( string s )
		{
			String []parts = s.Split();
			char op = ' ';
									
			foreach( string part in parts )
			{
				if( IsNum(part) )
					Push( double.Parse( part ) );
				else
					op = part[0];
			}
 
			switch( op )
			{
				case '+': return Add();
				case '-': return Sub();
				case '*': return Mult();
				case '/': return Div();
				case '%': return Mod();
				default: return Peek();
			}
		}
 
	}
 
	public class Form1 : System.Windows.Forms.Form
	{
		private Calculator calculator = new Calculator();
 
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox1;
		/// Required designer variable.
		private System.ComponentModel.Container components = null;
 
		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
 
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}
 
		/// Clean up any resources being used.
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
 
		#region Windows Form Designer generated code
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(152, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Enter Your Calculation";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(8, 56);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "Enter";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(8, 32);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 2;
			this.textBox1.Text = "1 + 1";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(184, 86);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.textBox1,
																		  this.button1,
																		  this.label1});
			this.Name = "Form1";
			this.Text = "Calculator";
			this.ResumeLayout(false);
 
		}
		#endregion
 
		/// The main entry point for the application.
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
 
		private void button1_Click(object sender, System.EventArgs e)
		{
			textBox1.Text = calculator.Calc( textBox1.Text ).ToString();
		}
	}
}

Flat View: This topic has 1 reply on 1 page
Topic: TcpChannel Previous Topic    

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use