Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a method that will return a string in the correct scientific notation given a precision parameter. The table below gives example input and output

Write a method that will return a string in the correct scientific notation given a precision parameter. The table below gives example input and output for the method. The input and output will both be strings. The precision will be an integer >= 0.

String - Value

Precision

Resulting String

------------------------------

--------------------

-------------------------

31536000

0

3E7

31536000

1

3.2E7

31536000

20

3.1536000E7

Not enough digits to have 20 places so only gives the answer with the greatest precision it can; in this case it is 7 places after the decimal.

-31536000

1

-3.2E7

3

Any value

3

0

Any value

0

public String covertSci(String number, int prec){

// precondition: prec >= 0

// postcondition: Returns a string that holds this number in

// scientific "calculator" notation with the

// prec number of digits. If prec >= NumDigits()

// it is reduced to the number of digits - 1.

This is what i have so far. I need help completing it.

1st class - Main.java

class Main{

public static void main(String[]args){

ScientificNot scinumber = new ScientificNot("31536000", 0);

System.out.println(scinumber);

}

2nd Class - ScientificNot.java

class ScientificNot{

char decimal '.';

String value;

int prec;

public ScientificNot(){

value = "";

prec = 0;

}

public ScientificNot (String value, int prec){

this.value = value;

this.prec = prec;

}

public String getValue(){

return value;

}

public ScientificNot convertSci(String value , int prec){

if (prec == 0){

value = (value.charAt(0) + "E" + value.substring(1,8).length());

if (prec == 1){

value = (value.charAt(0) + value.substring(0,1) + decimal + value.substring(1,2) + "E" + value.substring(2,8).length());

return value;

if (prec == 20){

value = (value.charAt(0)+ value.substring(0,1) + decimal + value.substring(1,2) + "E" + value.substring(2,8).length());

return value;

}

}

}

}

public String toString(){

return value + "," + prec;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions