I just have a quick question my instructor didn't really go over. His examples aren't helping me.
static double Q1(NormalDistribution distro, double x){
// return the z-score of x in the given distribution
}
It says to return the z-score of x in the given distribution. To not have to do all the math and "reinvent the wheel" each time, we are taught to import the library, I imported it, I'm just confused on how to call the class for the method. Please explain
External Library
package cse115.math;
import java.util.ArrayList;
public class NormalDistribution{
private double standardDeviation;
private double mean;
public NormalDistribution(double standardDeviation, double mean){
this.standardDeviation = standardDeviation;
this.mean = mean;
}
/**
* Creates a normal distribution given a data set.
*/
public NormalDistribution(ArrayList<Double> data){
double sum = 0.0;
for(double value : data){
sum += value;
}
double mean = sum / data.size();
double variance = 0.0;
for(double value : data){
variance += Math.pow(value - mean, 2.0);
}
variance /= data.size();
this.mean = mean;
this.standardDeviation = Math.sqrt(variance);
// yes, this method puts 3 free points on the table for the observant.
}
/**
* Returns the z-score of the provided value. Z-score is the number of standard deviations the value
* is away from the mean.
*/
public double zScore(double value){
return (value - this.mean) / this.standardDeviation;
}
public double getStandardDeviation(){
return this.standardDeviation;
}
public double getMean(){
return this.mean;
}
#Override
public String toString(){
return "{" +
"mean=" + mean +
", standardDeviation=" + standardDeviation +
'}';
}
}
You're going to need to create an instance of the external class within your main method.
NormalDistribution normalDistribution = new NormalDistribution(0,0);
you will then call the static function Q1 through the Classname which holds it, passing the NormalDistribution and the double x.
double value = Cse115.Q1(normalDistribution,1);
Whatever those are supposed to do is up to you, good luck.
Related
This question already has answers here:
Passing variable values from another method
(3 answers)
Closed 5 years ago.
I can't seem to grasp this
public static void AreaP(double Dia){
double R = Dia/2.0;
double A = PI * (R * R);
System.out.println("The area is: " + A);
public static void PPSI(double Price){
}
I have to find the price per square inch so I need to pass the area that I solved for in AreaP into the method PPSI and was wondering if there is a way I could do that because I know you can't pass methods into other methods.
public static double A=0;
public static void AreaP(double Dia){
double R = Dia/2.0;
A = PI * (R * R);
System.out.println("The area is: " + A);
System.out.println("The price per square inch is:+PPSI(50))
}
public static double PPSI(double Price){
return Price/A;
}
Making the variable 'A' static and outside the method will allow you to access it from anywhere inside the class or even outside(For.eg, Classname.A )
How about this:
private static final double PI = 3.1416;
public static double areaP(double dia){
double r = dia/2.0;
double a = PI * (r * r);
return a;
}
public static void getPricePerSquareInch(double price){
System.out.print(areaP(75)/price);
}
I have the following class in java :
public class Percentage
{
private double n;
Percentage (double n )
{
this.n=n;
}
public void setN()
{
this.n=n;
}
public double getN()
{
return n;
}
public double percntage ()
{
return this.n/100;
}
}
this Class Percentage will return a double value, but the problem is we can't make any mathematic operation with values like below:
public static void main (String args[])
{
Percentage p = new Percentage(5);
double b=1;
b=p*12; // this is error because the class Percentage in not of type double
}
is there someway to make Percentage of type double ?
That is an error because you are multiplying the Percentage object with double value.
The alternative is
public static void main (String args[])
{
Percentage p = new Percentage(5);
double b=1;
b=p.getN()*12;
}
You cannot make the class type double. You can perform your operation in the n value instead.
b = p.getN()*12;
you can't define a class as double, because double is a primitive type. What you can do is what the others user suggested:
p.getN();
It will return the double value you need.
No, you can't make it behave like a double, but (like BigDecimal) you can supply methods for performing the relevant operations.
Since your code seems to imply that n = 10 means 10%, i.e. a factor of 0.10, you could make methods like these:
public double of(double value) {
return value * this.n / 100d;
}
public double add(double value) {
return value * (100d + this.n)) / 100d;
}
and then use it like this:
Percentage p = new Percentage(10);
double input = 55;
double d1 = p.of(input); // 10% of 55 = 5.5
double d2 = p.add(input); // 55 + 10% = 60.5
So I have a class to compare the rating of a film. It implements the comparator class as seen below:
public class FilmComparator implements Comparator<Film> {
private Map<Film, List<Rating>> ratings;
public FilmComparator(Map<Film, List<Rating>> ratings) {
this.ratings = ratings;
}
#Override
public int compare(Film o1, Film o2) {
double average1 = average(o1);
double average2 = average(o2);
return average2 - average1; // I cant do this because it need to return an int
}
private double average(Film f) {
int sum = 0;
for (Rating r : ratings.get(f)) {
sum += r.getValue();
}
return sum / ratings.get(f).size();
}
}
As you can see, the average might not always be an integer. I am wondering how I would be able to have a more accurate compare. For example, I am having issues when the average returns 3.6 for one object but 3.0 for the other. To the compare method, the are the same but I need to show a difference. Is this possible?
Simple, let Double do the work for you. Do
return Double.compare(average1, average2); // or swap if desired
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed last month.
I am doing a very basic Complex Numbers class in Java but when I test my add and multiply functions I don't get the results I expect. I don't understand what is wrong or how to fix it.
When I run the program I get the following output:
a+b: ComplexNumber#1540e19d
a*b: ComplexNumber#677327b6
I should get the proper addition and multiplication of two complex numbers (following the rules of complex numbers of course)
Any help would be much appreciated! Thanks in advance.
Here is the code:
public class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double r, double i) {
real = r;
imaginary = i;
}
public double real() {
return real;
}
public double imaginary() {
return imaginary;
}
public ComplexNumber add(ComplexNumber c) {
double newr = real + c.real();
double newi = imaginary + c.imaginary();
return new ComplexNumber(newr, newi);
}
public ComplexNumber multiply(ComplexNumber c) {
double newr = real*c.real() - imaginary*c.imaginary();
double newi = real*c.imaginary() + imaginary*c.real();
return new ComplexNumber(newr, newi);
}
public static void main(String[] args) {
ComplexNumber c1 = new ComplexNumber(1.0, 2.0);
ComplexNumber c2 = new ComplexNumber(-1.0, 0.5);
String c1plusc2 = c1.add(c2).toString();
String c1timesc2 = c1.multiply(c2).toString();
System.out.println("a+b :" + c1plusc2);
System.out.println("a*b :" + c1timesc2);
}
}
You need to override the toString method in the ComplexNumber class:
#Override
public String toString() {
return real + " + i*" + imaginary;
}
Your .add() & .multiply() methods return a ComplexNumber object. By default,
System.out.println("a*b :" + c1.multiply(c2));
evaluates to
System.out.println("a*b :" + c1.multiply(c2).toString());
The toString() method is inherited from the Object class (since all classes inherit from Object). And since you're not overriding it in the ComplexNumber class, you get the default return value from Object's toString() method:
ClassName#hashCodeOfTheObject
EDIT:
toString() returns a String. Change
ComplexNumber c1plusc2 = c1.add(c2).toString();
ComplexNumber c1timesc2 = c1.multiply(c2).toString();
to
String c1plusc2 = c1.add(c2).toString();
String c1timesc2 = c1.multiply(c2).toString();
Output:
a+b :0.0 + i*2.5
a*b :-2.0 + i*-1.5
Given the following example code, please help me answer the following questions with hints
public class Coin
{
private String myColor;
private int mySideOne;
private double mySideTwo;
public Coin(String Color, int SideOne, double SideTwo)
{
myColor= Color;
mySideOne = SideOne;
mySideTwo = SideTwo;
}
//accessors getColor(), getSideOne(), and getSideTwo()
}
public class Total
{
private int myNumCoins;
private Coin[] moneyList;
//constructor
public Total(int myCoins)
{
myNumCoins = numCoins;
moneyList = new Coins[numCoins]
String color;
int mySideOne;
double mySideTwo;
for (int i = 0; i<numCoins; i++)
{
}
}
**
Question:
**
//Returns total amount for Coins
public double totalMoney()
{
double total = 0.0;
/* code to calculate
return total;
}
}
Which represents correct / code to calculate amount */ in the totalMoney method?
A. for (Coin t: moneyList)
total+= moneyList.getSideTwo();
B. for (Coin t: moneyList)
total+=t.getSideTwo();
I think A is right because the "t" in B. doesn't exist in the code. How am I wrong?
Let's evaluate the code using A.:
public double totalPaid()
{
double total = 0.0;
for (Ticket t:tickList)
total+= tickList.getPrice();
return total;
}
tickList is an array of Tickets. An array is an object which only has a static final field called length. So, tickList cannot have getPrice. This means, option A doesn't compile.
Let's evaluate the code using B.:
public double totalPaid()
{
double total = 0.0;
for (Ticket t:tickList)
total+=t.getPrice();
return total;
}
Here you state:
I think A is right because the "t" in B. doesn't exist in the code. How am I wrong?
In fact, t is a variable declared and used in the enhanced for loop statement. t is from type Ticket and it will take the value of each Ticket object reference stored in tickList. The enhanced for loop can be translated to this form for arrays:
for (int i = 0; i < tickList.length; i++) {
Ticket t = tickList[i];
//use t in this scope
//in this case, it's used to accumulate the value of total
total += t.getPrice();
}
Which makes B as the solution for this problem.
The answer is B because you declare t in your loop when you say Ticket t. The loop iterates the ticketList and t will stand for each Ticket in the list.