Method That Returns Positive / Negative Or Zero Depending On Sum Of Two Variables?

Dec 10, 2014

I'm taking a class in object oriented programming and we have a task to write a method that returns positive, negative or zero depending on the sum of two variables.

I've had a go at it and i've got to a certain point but i'm struggling to get past this on error - return outside method.

public class Test {
public int sumArgs;
public int arg1;
public int arg2;

[Code] ....

The error occurs on the line 'return "Positive";

View Replies


ADVERTISEMENT

Determine Positive And Negative Values Have Been Read

Feb 4, 2015

The program work somehow, but it can't count the first input when user key in.

import java.util.Scanner;
public class DetermineValues {
public static void main( String[] args ) {
int sum;
int minus;
int data;

[code]....

View Replies View Related

Rearranging Array Values From Negative To Positive

Feb 7, 2015

I have a problem where I am trying to re arrange the values in an array from negative to positive. I have it re arranged but I cannot figure out how to re arrange them in numerical order. I have to use O(n) and O(1) operations.

Java

import java.util.Arrays;
public class Task7 {
public static void main(String[] args){
int[] numbers = {-19, 6, 34, -3, -8, 23, 5, 678, -45, -12, 76}; //array of positive and negative numbers
int next = 0; //in no particular order

[Code] .....

View Replies View Related

Generate Random Number Between Two Values That Can Be Negative Or Positive

Apr 30, 2015

I am trying to make a method that generated a random number between two values that can be negative or positive.

So:

rand(-0.2, 0.2);

would give one of these: -0.2, -0.1, 0, 0.1, 0.2

View Replies View Related

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings?

Feb 3, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

This is what i have but it only adds positive numbers and it doesn't subtract problems like 7.05-8.96. Also some of it was what our teacher gave us like alignwhole method

import java.util.Scanner; 
public class Number{
private String whole;
private String decimal;
private String sign;
  public static void main (String[] args){
 System.out.println("Enter two numbers");

[code]....

View Replies View Related

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings

Feb 5, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

The below code is what our teacher gave us to start with, but it needs to add and subtract positive or negative numbers of any length. This code only adds positive numbers. Need to write code for subtraction .

Java Code:

import java.util.Scanner;
public class Number{
private String whole;
private String decimal;
private String sign;

[Code] .....

View Replies View Related

Print Isosceles Triangle Depending On Variables Height

Nov 14, 2014

The program needs to print an isosceles triangle depending on variables Height, BorderSymbol and Interior symbol, how to do that. Here is what i did so far :

{
int height = 5;
int i = 0;
int count = 0;
String line = "";
 
[code]....

and the output is a blanc page nothing on the screen only spaces...

View Replies View Related

Generate Two Arrays - One With All Positive Numbers And Another With Negative Numbers

Mar 10, 2015

Create an integer array with 10 numbers, initialize the array to make sure there are both positive and negative integers. Write a program to generate two arrays out of the original array, one array with all positive numbers and another one with all negative numbers. Print out the number of elements and the detailed elements in each array.

public class problem3 {
public static void main(String[]args){
int[] numbers = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5};
for (int i = 0; i<numbers.length;){
if(i>0){
System.out.println(numbers);
}
else
System.out.println(numbers);
}
}
}

View Replies View Related

Change Negative Number To Positive Number?

Nov 20, 2014

I am 4 weeks into my Intro to Java course and I am having a bit of trouble with my code. I need to make a program that will take a user inputted number, space the numbers out, then add them to a total sum. What I am having a hard time with is when I enter a negative number. I can't figure out what I need to do to have my program ignore the "-" in the string.

import java.util.*;
public class Week4_Programming_Problem
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int number, digit, sum=0;
char digitPos;

[code]....

View Replies View Related

Writing A Method That Returns Words Without Four Letters Back To Main Method

May 27, 2014

I have to write a method called censor that gets an array of strings from the user as an argument and then returns an array of strings that contains all of the original strings in the same order except those with length of 4. For example if cat, dog, moose, lazy, with was entered, then it would return the exact same thing except for the words with and lazy.

Currently, my code so far just prints [Ljava.lang.String;@38cfdf and im stuck.

import java.util.Scanner;
 public class censorProgram {
public static void main (String args[]){
Scanner input = new Scanner (System.in);
System.out.println ("How many words would you like to enter?");
int lineOfText = input.nextInt();
 
[Code] ....

I have to make new string array in the method and return words without four letters in the main method

View Replies View Related

Create A Method That Returns A New Array

Mar 4, 2014

I need to create a method that returns a new array containing the componentwise sum of its arguments(if length is the same). For instance, if the input arrays are {0,1, 2} and {2, 2, 3} then the output is {0+2, 1+2, 2+3}, i.e. {2,3,5}.If the input arrays have different numbers of elements, the method should return null.

I came with something like this, however i dont know how to make a copy of an array from two arrays. My code obviously wont compile. package whatever;

import java.util.Arrays;
public class hhhh {
public static void main(String[] args) {
double [] a = {1,2,3};
double [] b = {2,3,4};

[code]...

View Replies View Related

Split Method In Java - How Does Negative Limit Works

May 16, 2015

I am not able to understand how split method in java works

If I have

value.toString().split(";", -6);
or
value.toString().split(";", -2);

what will be the difference...basically , i want to know how does negative limit in a split works...

View Replies View Related

Method That Returns Largest Value Of Array Of Doubles?

Jan 26, 2015

Write a Java method that returns the largest value of an array of doubles passed to the method as an argument.

Back into java wasn't sure how to do it for doubles did one in the main for integers and then added a method changed from int to double and now i'm lost as go why its not working.

package kickstarter9;
public class Kickstarter9 {
public static void main(String[] args){
double myList;
double[] myList = {6.0, 4.1, 2.4, 6.8, 1.9, 9.4, 2.8, 4.6, 9.3};
// find the largest value in the list

[Code]...

View Replies View Related

Queues - IsEmpty Method Only Returns False

Jan 29, 2015

My isEmpty method only returns false. Is something wrong? I printed the empty and not empty for testing purposes.

//determines if there are any items in the queue
public boolean isEmpty() {
if (front == -1 && rear == -1) {
System.out.println("empty");
return true;
} else {
System.out.println("not empty");
return false
}
}

View Replies View Related

Static Method That Requires No Arguments And Returns No Value

Sep 30, 2014

I am doing a homework assignment and I am clueless as to what I did wrong or supposed to do in terms of placement of coding. If someone could dumb down their response and tell me what I did wrong

This is what I supposed to do:

1.) Type the following shell for the class:

public class ParadiseInfo
{
}

2.) Between curly braces of the class, indent a few spaces and create the shell for the main() method:

public static void main(String[] args)
{
}

3.) Between the braces of the main()Method, insert a call to the displayInfo() method:

displayInfo();

4.) Place the displayInfo() method outside of the main() method, just before the closing curly brace for the ParadiseInfo class:

public static void displayInfo()
{
system.out.println ("Paradise Day Spa wants to pamper you.");
system.out.println ("We will make you look good.");
}

This is what I attempted to do: I know it is wrong I am just clueless on where to put the code and why

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
public static void displayInfo();
}
system.out.println("Paradise Day Spa wants to pamper you.");
system.out.println("We will make you look good.");
}

--- Update ---

I have also tried this:

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
}
public static void displayInfo();
{

[Code]...

The very last attempt I only ended up with one error which is:

G:ParadiseInfo.java:3: error: invalid method declaration; return type required
displayInfo();
^
1 error

Tool completed with exit code 1

Java is very new to me and I have no clue on what I am doing wrong.

View Replies View Related

Using Method Calls With Returns But It Keeps On Showing Errors

Oct 12, 2014

I am trying to use method calls with returns but it keeps on showing errors. The errors say class, interface, or enum expected. I realize this error occurs if there is issue with declaring class - but i can't seem to find the error. I will post the code that shows error.
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class FuelCost extends JFrame
{
// declarations
Color black = new Color(0, 0, 0);

[Code] ....

The error states:

C:UsersPriti BhattaraiDesktopCCAC JavaFuelCost.java:290: error: ';' expected
public double calculateCostToFillTank()
^
C:UsersPriti BhattaraiDesktopCCAC JavaFuelCost.java:292: error: class, interface, or enum expected
return (averageMilesPerTank / milesPerGallonRating) * currentPricePerGallon;

[Code] ....

12 errors

Tool completed with exit code 1

View Replies View Related

Swing/AWT/SWT :: GetGraphics Method Returns Null

Aug 23, 2007

The getGraphics() method in my constructor is returning a nullPointerException and I can't figure out why. This class is extending a JPanel and I thought calling super() would fix it because it makes sense that the JPanel would initialize its graphics object in the constructor, but that didn't fix it. The line that produces the error is in bold. I've narrowed it down to the getGraphics() method is returning null, so the next line that calls the graphics object is actually the one that produces the error.

int leftMargin = 2, rightMargin = 2, topMargin = 2, bottomMargin = 2;
int lineSpacing = 1;
int currentX = leftMargin, currentY = topMargin;
Font defaultFont, customFont;
public HTMLFrame(){
super();

[Code] ....

print(String) is a method I made that draws lines of text on the graphics object, similar to System.out.println(String) in the command prompt.

View Replies View Related

Method That Returns Non-Numeric Values - Switch Statement

Feb 23, 2014

I am trying to write out a program that takes numerical input from the user and converts it to a date using the English month name. I am experimenting with the method of a "switch" statement without using the "break" clause. However, I seem to be missing something, as Eclipse is telling me I have a syntax error with my block. My curly braces seem properly placed. Also, I made sure to follow guidelines to make my code fit on the screen and remain easy to read.

import acm.program.*;
public class MethodsThatReturnNonNumericValues extends ConsoleProgram {
public void run() {
int month=readInt("Enter month number");
int day=readInt("Enter day");
int year=readInt("Enter year");

[Code] ....

View Replies View Related

Method That Returns Largest Object In Array Of Objects

Apr 4, 2015

This is what I have to create : Write a method that returns the largest object in an array of objects. The method signature is:

public static Object max(java.lang.Comparable[] a)

All the objects are instances of the java.lang.Comparable interface. The order of the objects in the array is determined using the compareTo method.

Write a test program in the main method that creates an array of ten Strings, an array of ten Integers, and an array of ten java.util.Dates, and finds the largest string (i.e. in the alphabetical order), Integer, and Date in the arrays.

Name your java class Max and your java file Max.java.

I am struggling a bit with this code as I am sure you can see, and am at loss. I have never used the compareTo method. Am I doing this right, or on the right track with my code?

public class Max implements Comparable {
public static Object max(java.lang.Comparable[] a) {
java.lang.Comparable tempObj = null;
for (int i = 0; i < a.length; i++) {
if (a[i].compareTo(tempObj) > 0)

[Code] ....

View Replies View Related

Write A Method That Returns Unicode Values Of A String?

Oct 25, 2014

I must write a method that accepts a string and returns an int. The method should sum the unicode values of each character, so the string "ABC" should return 198. Also the only string class methods I'm aloud to use are length, charAt, and substring. I just don't know how the get the Unicode value.

View Replies View Related

Creating A Static Method That Requires No Arguments And Returns No Values?

Sep 28, 2014

I am doing an assignment and I am clueless as to what I did wrong or supposed to do in terms of placement of coding.

This is what I supposed to do:

1.) Type the following shell for the class:

public class ParadiseInfo
{
}

2.) Between curly braces of the class, indent a few spaces and create the shell for the main() method:

public static void main(String[] args)
{
}

3.) Between the braces of the main()Method, insert a call to the displayInfo() method:

displayInfo();

4.) Place the displayInfo() method outside of the main() method, just before the closing curly brace for the ParadiseInfo class:

public static void displayInfo()
{
system.out.println ("Paradise Day Spa wants to pamper you.");
system.out.println ("We will make you look good.");
}

This is what I attempted to do:

I know it is wrong I am just clueless on where to put the code and why

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
public static void displayInfo();
}
system.out.println("Paradise Day Spa wants to pamper you.");
system.out.println("We will make you look good.");
}

--- Update ---

I also tried it this one and ended up with 1 error..

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
}
public static void displayInfo();
{
system.out.println("Paradise Day Spa wants to pamper you.");
system.out.println("We will make you look good.");
}
}

View Replies View Related

Static Method - Take Three Boolean Inputs And Returns True If Odd Number

Jun 28, 2014

Write a static method odd() that takes three boolean inputs and returns true if an odd number of inputs are true, and false otherwise.

View Replies View Related

Returning Multiple Variables From A Method

Nov 18, 2014

Now that I have figured out how to return a single variable from a method I am on to the next step, I need to return 2 or 3 variables from a method. I have searched the internet on how to do this to no avail (am I doing the wrong search? I do not know) but what I need to do is return a second (and eventually a third variable)

Here is what I have

private static String Engine() {
String engine = "";
int enginePrice = 0;
System.out.println("Choose a engine size:");
System.out.println("[4] cylinder");

[Code] .....

Which does not work.

View Replies View Related

Can Method Variables Be Shared Between Users?

Aug 27, 2014

Can method variables be shared between users?

I was asked this on an interview and I didn't know what to make of it.

View Replies View Related

Method That Returns New Array By Eliminating Duplicate Values In Array

Jun 15, 2014

Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: public static int[] eliminateDuplicates(int[] list). The thing is that I found the working solution that is written below, but how it works. How to eliminateDuplicates method done with flag and flag2.

Here is the code:

Java Code:

import java.util.Scanner;
public class Exercise06_15 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Enter ten numbers: ");

[code]....

View Replies View Related

Class Method Which Take String And Returns A String In Reversed Version

Dec 16, 2014

i am trying to write a class method which will take in a string and returns a string which is the reversed version of that string. it compiles fine but when i try to run it it states Main method not found in class StringReverse,please define the main method as public static void main(String[]args). I am new to java and cannot figure out

import javax.swing.JOptionPane;
public class StringReverse {
public String reverseString(String str){
JOptionPane.showInputDialog(null,"Please enter word");
char c = str.charAt(str.length()-1);
if(str.length() == 1) return Character.toString(c);
return c + reverseString(str.substring(0,str.length()-1));}}

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved