hi, I have one hw about linear interpolation on java...we can not use arrys to d
ID: 3591424 • Letter: H
Question
hi,
I have one hw about linear interpolation on java...we can not use arrys to do this hw, I do not kown how to make the calculation for find the line and calcule m/v.. this is the hw...
Task: Develop an Interpolator Program
Objectives:
control structures
console-based user input using Scanner class
writing complete programs using two classes
precondition testing
A scientific experiment measures the mass and volume of an experimental substance as a function of time. The experiment, however, can only measure a very few data points, as seen in the plots below:
As you can see, there are only 3 measured data points for the mass measurement, and 4 measured data points for volume. You need to write a program that stores these 7 data points but can calculate m and v for most values of t. For any t between the data points, we call this linear interpolation. For t < smallest t data point or > largest t data point, we call this linear extrapolation.
The program should allow the user to enter any value of t, then display either:
mass density as calculated by m / v
an error message if t is outside of the upper or lower range of validity
The data points from the measurement are:
Mass: (1.1, 5.3) ( 2.75, 7.2) (4.0, 6.15)
Volume: ( 1.5, 0.5) (3.2, 0.8) ( 4.3, 1.5) (5.5, 2.75)
User Interface Specifications:
This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows:
When the program starts, display a 1 to 2 line short description
Prompt the user to input an option
Ask the user for the required input and then display the results
Display the prompt again to do another calculation
if the user enters the value -999 for time, the program stops and displays a termination message
Code Specifications:
NEW! View the Java Documentation!
The code must be written into two classes: UserInterface, where the main() method displays an introduction, gets user input, calls the calculation methods from the Interpolator class, and tests for invalid return values. It should keep prompting the user for more input values until the user enters -999.
The Interpolator class does not need a main() method, but it must contain two methods: computeVolume(double), and computeMass(double). This class must also contain constants that describe all 7 data points provided above. The class variables should be private, but the methods should be poublic. If either method attempts to calculate outside of the valid range, it should return -1. This return value signals to the calling method that the input is not valid.
Each method requires heading comments, and each method requires comments describing parameters and return values.
Interpolation Analysis:
Given two points (x1, y1) and (x2, y2), you can find a line that passes through them in the form y = mx + b. However, if you read this Wikipedia article (Links to an external site.)Links to an external site., you can find a more useful form. Either way, linear interpolation uses the line that connects two points to produce approximate values of the y variable for any x.
As seen in the diagrams above, you can also use the lines to estimate y values outside the measured x range. This is called linear extrapolation. However, for any set of (x, y) coordinates, linear extrapolation is only valid within a certain range on the x axis. On the second plot above, volume vs. time, you can see that the interval width ( x2 - x1 ) is used as the limit for values less than x1. The same kind of limit applies for values above the largest x data point.
Sample Data:
this is my current codes:
import java.util.Scanner;
public class UserInterface
{
public static void main(String args[]){
Interpolator interp = new Interpolator();
System.out.println("hi,this program does....");
Scanner sc= new Scanner(System.in);
System.out.println("Please enter any value of t ");
double Valueoft = sc.nextDouble();
interp.Calculation(Valueoft);
}
}
public class Interpolator
{
//double[] x ={1.1,2.75,4.0,1.5,3.2,4.3,5.5};
//double[] y ={5.3,7.2,6.15,0.5,0.8,1.5,2.75};
double x1=1.1;
double x2=2.75;
double y1=5.3;
double y2=7.2;
public static void Calculation (double x)
{
if (x<0 && x>6){
System.out.println("Invalid - out of range");
}
}
}
Explanation / Answer
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Userinterface
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of time");
double n =sc.nextDouble();
Interpolation d=new Interpolation();
while(n!=-999)
{
if(n<0||n>=6)
{
System.out.println("Invalid - out of range");
}
else{
double m=d.computemass(n);
double v=d.comuputevolume(n);
System.out.println(m/v);}
System.out.println("Enter the value of time");
n =sc.nextDouble();
}
}
}
class Interpolation//This class contains two function computemass and computevolume
{
double m[]=new double[]{ 5.3,7.2,6.15};
double t[]=new double[]{1.1,2.75,4.0};
double v[]=new double[]{.5,.8,1.5,2.75 };
double t1[]=new double[]{1.5,3.2,4.3,5.5};
public double computemass(double n)//In this function we are finding the value of mass by equation of line.
{
double y=0.0,x=n ;
if(n==t[0])
{
y=m[0] ;
}
if(n==t[1])
{
y=m[1];
}
if(n==t[2])
{
y=m[2];
}
if(n<t[0]||n>t[0]&&n<t[1])
{
y=m[0]+(m[1]-m[0])*(x-t[0])/(t[1]-t[0]);
}
else if(n>t[1])
{
y=m[1]+(m[2]-m[1])*(x-t[1])/(t[2]-t[1]);
}
return y ;
}
public double comuputevolume(double n)//Here we are computing the value of volume by equation of line
{
double x=n,y=0.0 ;
if(n==t1[0])
{
y=v[0];
}
if(n==t1[1])
{
y=v[1];
}
if(n==t1[2])
{
y=v[2];
}
if(n==t1[3])
{
y=v[3];
}
if(n<t1[0]||n>t1[0]&&n<t1[1])
{
y=v[0]+(v[1]-v[0])*(x-t1[0])/(t1[1]-t1[0]);
}
else if(n>t1[1]&&n<t1[2])
{
y=v[1]+(v[2]-v[1])*(x-t1[1])/(t1[2]-t1[1]);
}
else if(n>t1[2])
{
y=v[2]+(v[3]-v[2])*(x-t1[2])/(t1[3]-t1[2]);
}
return y ;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.