Solved

output format

Posted on 2008-06-26
3
194 Views
Last Modified: 2010-03-30
please help me to display double type output. I know it round off, but i don't know now to fix it. Thanks.
import java.util.*;

import java.io.*;

import java.text.DecimalFormat;

import java.text.NumberFormat;
 

// calculate value of x, y, z

class Calculation

{

	double a, b,  c,  d;

	double t;
 

	public double findPoint ()

	{

		if( a == 0 && b ==0 )       //return z

		{

			t = -d/c;
 

			NumberFormat formatter = new DecimalFormat("##");

			String s = formatter.format(t);

			System.out.print("z is " + s + "\n");
 

			return(t);

		}

		else if( a == 0 && c ==0 )  //return y

		{	t = -d/b;
 

			NumberFormat formatter = new DecimalFormat("##");

			String s = formatter.format(t);

			System.out.print("y is " + s + "\n");
 

			return(t);

		}

		else                        //if( b == 0 && c ==0 )  // return x

		{

			t= -d/a;
 

			NumberFormat formatter = new DecimalFormat("##");

			String s = formatter.format(t);

			System.out.print("x is " + s + "\n");
 

			return(t);

		}

	}
 

	void assignValue (double first, double second, double third, double fourth)

	{

		a = first;

		b = second;

		c = third;

		d = fourth;

	}

}
 

class Demo

{

	public static void main ( String[] args)

	{
 

		double a, b, c, d;

		double x, y, z;
 

		Calculation item1 = new Calculation();
 

		// supposed plane equation is 0.3x + 0.5y + 0.7z + 0.9 = 0

		//                              a      b      c      d
 

		item1.assignValue(0.3,0.0,0.0,0.9); // get x => y = z = 0

		x = item1.findPoint();              //x = -3
 

		item1.assignValue(0.0,0.5,0.0,0.9); // get y => x = z = 0

		y = item1.findPoint();              // y= -1.8
 

		item1.assignValue(0.0,0.0,0.7,0.9); // get z => x = y = 0

		z = item1.findPoint();				// z= -1.2858
 

	}

}

Open in new window

0
Comment
Question by:valleytech
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
Do you want 2 decimal places? If so:
        public double findPoint2() {

                if ((a == 0) && (b == 0)) //return z

                 {

                        t = -d / c;
 

                        System.out.printf("z is %.2f  n", t);
 

                        return (t);

                } else if ((a == 0) && (c == 0)) //return y

                 {

                        t = -d / b;

                        System.out.printf("y is %.2f  n", t);
 

                        return (t);

                } else //if( b == 0 && c ==0 )  // return x

                 {

                        t = -d / a;

                        System.out.printf("x is %.2f  n", t);
 

                        return (t);

                }

        }

Open in new window

0
 

Author Comment

by:valleytech
Comment Utility
go it. Thanks for quick response.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now