Pair Of Two Numbers - Return Average / Distance / Maximum And Minimum Value
Sep 27, 2013
I have been having difficulty with the weeks concepts in my online Java class, the program is to be as followed:
For this exercise you will implement a class called Pair, that represents a pair of two numbers.The Pair class should include the following constructor and methods:
CONSTRUCTORS
public Pair(double num1, double num2) -- Creates an object that represents a pair of double values
METHODS
public double getAverage() -- Returns the average value of the two numbers
public double getDistance() -- Returns the absolute vale of the distance between the two numbers
public double getMaximum() -- Returns the maximum value of the two numbers
public double getMinimum() -- Returns the minimum vale of the two numbers
Write a class called PairTest that tests your Pair implementation. The PairTest should prompt the user for the two values, create a Pair object with the values and then print the average, distance, maximum, and minimum of the pair. The input / output should look like the following:
Enter the first number: 5.5
Enter the second number: 3.0
NOTE: For this exercise, your solution should not use any conditional statements. Instead you should use the methods provided by thejava.util.Math.
So far I have:
import java.lang.Math;
import java.util.Scanner;
public class Main
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
{
System.out.println("Please enter a value for the first number");
1. Create a program that will return the maximum and minimum numbers in the elements of ONE-dimensional integer array. Save it as MaxMin_OneDim.java
2. Create a program that will return the maximum and minimum numbers in the elements of each row in a TWO-dimensional integer array. Save it as MaxMin_TwoDim.java
3. Write a program PrintPattern which prompt a user to enter a number and prints the following patterns using nested loops (assumed user entered number is 8 output is:)
1 .... 87654321
12 .... 7654321
123 .... 654321
1234 .... 54321
12345 .... 4321
123456 .... 321
1234567 .... 21
12345678 .... 1
(Without the dots, i just put them to give spaces)
Write method distance to calculate the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double. Incorporate this method into an application that enables the user to enter the coordinates of the points.
Hints:
- The distance between two points can be calculated by taking the square root of
( x2 - x1 )2 + ( y2 - y1 )2
- Use Math class methods to compute the distance.
- Your output should appear as follows:
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
On Windows type <ctrl> z then press Enter
Or Enter X1: 1
Enter Y1: 1
Enter X2: 4
Enter Y2: 5
Distance is 5.000000
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
i need to calculate the minimum and the maximum, actually it seems to be easy but, the minimum should be the smallest number but 0..this is my code
Java Code:
Scanner s = new Scanner (System.in); int max = 0 ; int min = 0 ; System.out.println(" Please enter 3-5 numbers"); int a = s.nextInt(); int b = s.nextInt(); int c = s.nextInt(); int d = s.nextInt(); int e = s.nextInt();
I am doing a homework assignment for a class I'm taking and writing a java program that finds the smallest and largest number in an array entered by the user. For some reason no matter what I enter as the smallest number it returns 0 as the smallest. I'm not sure what I have done wrong. Here is my code:
import javax.swing.JOptionPane; public class MinimumAndMaximumJamesBulow { public static void main(String[] args) { int minimum = 0; int maximum = 0;
I am trying to write a TreeMap that can hold a max of 20 colors and a minimum of 8. I have a while loop using pollLastEntry to limit the max but I can't figure out how to set the minimum. The hex number is the map's key and the color name is the value. I tried to use entrySet() and iterator to just double the size of the map but map can't have multiple keys with the same value. It also seems that to set a minimum would require some kind of further input(which I'm trying to avoid) of colors and their hex numbers.
//Method to hard code the colors into the map public TreeMap<String, String> cm() { //Color Map <Hex number, Color name> //Uses a TreeMap to keep all the colors organized by key TreeMap<String, String> cMap = new TreeMap<String, String>(); cMap.put("FFFF00", " Yellow");
So I'm learning java from a website and I was tasked with creating a simple program which allows the user to enter a series of integers, then finally when they decide to input a non-integer the program will print the maximum and minimum values of the integers they entered. So for example if they entered 5, 4, 3 and 2 then enter a non-integer the program would output 5 (maximum value), then 2 on a new line (minimum value).
Here is my code:
import java.util.Scanner; public class MaxMinPrinter { public static void main(String[] args) { Scanner in = new Scanner(System.in); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE;
[Code] ....
And this is what the output looks like:
Actual output ------------------------------------------- Enter an integer: 5 - 10 - - Enter an integer: -4 - 8 - -6 - - Enter an integer: 11 - -1 - - Enter an integer: q - -1 - -6
When it's supposed to look like this:
Expected output ------------------------------------------- Enter an integer: 5 Enter an integer: 10 Enter an integer: -4 Enter an integer: 8 Enter an integer: -6 Enter an integer: 11 Enter an integer: -1 Enter an integer: q 11 -6
I am working on this rental program and got stuck in the last part. The program is about renting a car; after completing the rent process it displays the user who spent maximum and minimum rent. This is where I can't proceed. The program displays maximum values for both max and min. Here's the code I have written:
import java.util.Scanner; // program uses Scanner public class CarRentalTest { public static void main( String[] args ) { System.out.println("Welcome to Rental Portal"); Scanner input = new Scanner( System.in ); // create Scanner to obtain input from command window CarRental details=new CarRental();
I was given some code by a professor to add some features to as part of an assignment. However, the code itself doesn't seem to work.
import java.util.HashSet; import java.util.InputMismatchException; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; public class DijkstraPriorityQueue
[Code] ....
The method to find minimum distance is nonfunctional...I receive an error that the types are incompatible. I can't do the assignment if the base code doesn't work to begin with...
I am working on this new project where we are using the great distance formula but every time I run my ending result is NaN. I was researching, and people say its because you divide by 0. I think I have my formula correct.
Java Code:
public class testingFormula { public static void main(String[] args) { double lat = 34.01858902; double lon = -118.2817688; double lat2 =33.94530869; double lon2 = -118.3994904;
I have to return the minimum and maximum value entered by the user, so far the program returns the maximum but the minimum stays at 0. I assigned 0 to the min and max variables. I believe that is part of the problem, but why do I get an accurate answer on the max if both variables are assigned to 0?
int min = 0; int max = 0; int option = JOptionPane.YES_OPTION; while ( option == JOptionPane.YES_OPTION){ String dataString = JOptionPane.showInputDialog("Enter an integer"); int data = Integer.parseInt(dataString); if ( min > Integer.parseInt(dataString)) min = Integer.parseInt(dataString); if ( max < Integer.parseInt(dataString)) max = Integer.parseInt(dataString); option =JOptionPane.showConfirmDialog(null, "Continue?"); } JOptionPane.showMessageDialog(null, "Minimum: " + min + " Maximum: " + max); }
depth is a variable for the row in the triangle. My problem is that i need the solution to be recursive and i am having trouble doing this. So far i have
public static int findMax(int[][] array,int depth) { if (depth==0) return array[0][0]; else if }
a. Write a Java program to input 10 integer numbers into an array named fmax and determine the maximum value entered. Your program should contain only one loop, and the maximum should be determined as array element values are being input. (Hint: Set the maximum equal to the first array element, which should be input before the loop used to input the remaining array values.)
b. Repeat 1a, keeping track of both the maximum element in the array and the index number for the maximum. After displaying the numbers, display these two messages:
The maximum value is: _________ This is element number __________ in the list of numbers
Have your program display the correct values in place of the underlines in the messages.
c. Repeat 1b, but have your program locate the minimum value of the data entered.
I did parts a and b but for part see i just want to know if i did it correctly or not
import java.util.Scanner; public class MinimumValueArray { public static void main(String[] args) { //Variable Declaration Scanner keyboard = new Scanner(System.in); int size = 10;
[Code] ,.....
When I run it i get this The minimum value is 0.0
The element that holds the value is 0 right away. is this right for the minimum or am i supposed to enter values and it will display the minimum value like in parts a and b wit the maximum? will the minimum just always be 0 or ?
For my project, I am trying to return the method average so the program can calculate the average after grades are enter. I'm not sure what I am doing wrong and how I should improve it. My code is below. This is my error message: "Project.java:71: error: '.class' expected".
import java.util.Scanner; public class Project { public static void main(String[] args)
How to create a simple program that can input number from 1 to n. and display the numbers which input by the user and also display the sum and average of it.
Ex. Please input a number: 1 2 3 4 5 6 7 8 9 10
the sum numbers of 12345678910 is 55 the average number of 12345678910 is ______,
(Count positive and negative numbers and compute the average of numbers). Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number.
I moved the different boolean statements around, but I'm not getting the sentinel value to end the run. It continues to let me add integers endlessly. The code I wrote is below:
package exerciseFourOne; import java.util.Scanner; public class AverageOfIntergers { public static void main(String[] args) { // TODO Auto-generated method stub int positive = 0; // number of positive integers int negative = 0; // number of negative integers int sum = 0; // value of sum of integers
public static int getSum(int[] data) { int sum = 0; for (int i = 0; i < data.length; i++) { sum += data[i]; } return sum;
[code]....
The input is the following arary (Its from the Junit test that fails this): [Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE].I get an average of 0.0 when it should be -0.2.
For some reason, the averages aren't calculating correctly. I'm aware I need to convert to double if I'm dividing by an odd amount but that itself isn't the issue. It's just simple easy divisible numbers that are not being calculated correctly. For instance, (10 + 10 + 10 + 50) / 4 = should be 2 but I get 10.
Here is the code:
import java.util.Scanner; public class Avg { public static void main(String[] args){ Scanner inputGains = new Scanner(System.in); int userInput = inputGains.nextInt();
Write a program that will read unspecified numbers of scores . The program will output the average of these scores and the scores that are below and above the average.
Example.. 1 2 3 4 5 Average is 3 Below 1 2 Above 4 5
I have been assigned to write a program that has a user input random numbers then the program is to sort them in different ways. I have the coding for the different sorts but, I have an error saying that I am missing a return statement in the "Bubble" method. I am trying to return "arr[i]" in the "for loop" which gives me this error, and when I try to take the "return arr[i]" outside of the "for()" loop the error reads the it cannot locate variable "i".
import java.awt.* ; import java.awt.event.*; import javax.swing.*; public class SwingSorts extends JFrame implements ActionListener { JRadioButton bubble; JRadioButton selection;
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth. at the moment my code looks like this:
// File1: public class date { public int day; public int month; public int year; }
// File 2: public class monthlength { public int mlong(date X) { int t; t = X.month; if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12) { return 31; } if(t == 4 || t == 6 || t == 9 || t == 11) {return 30;} } }
What are the x- and y-coordinates of the Points referred to as p1, p2, and p3 after the following code executes? Give your answer as an x-y pair such as (0, 0). Recall that Points and other objects use reference semantics.
PHP Code:
Point p1 = new Point(); p1.x = 17; p1.y = 9; Point p2 = new Point(); p2.x = 4; p2.y = -1; Point p3 = p2; p1.translate(3, 1); p2.x = 50; p3.translate(-4, 5); mh_sh_highlight_all('php');