Arithmetic Methods Whose Main Method Holds Two Integer Variables

Jan 15, 2015

a. Create an application named ArithmeticMethods whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayNumberPlus10(), displayNumberPlus100(), and displayNumberPlus1000(). Create each method to perform the task its name implies.

b. Modify the ArithmeticMethods class to accept the values of the two integers from a user at the keyboard.

View Replies


ADVERTISEMENT

Calling Multiple Methods Into Main Method

Sep 30, 2014

This current one is to calculate a planes holding pattern. I have to write a method to prompt user to enter speed in knots, then it converts it to km/hr. Another method to calc. pattern width using the speed, another method to calc. pattern length, than a main method which would call and print out the speed in knots, speed in km, pattern width and length.My current problem is on the second method. It works in that I can enter the values and it gives me the correct answers, however it's asking me to enter the speed twice, instead of just once. Anything I try just results in errors and won't compile.

import java.util.Scanner ;
//main method
public class TitleRemoved {
public static void main(String[] args) {
double airSpeedKm = airSpeedOts () ;
System.out.println("That speed is " + airSpeedKm + " km/hr.") ;

[code].....

I want my code to not only work, but be organized and easily readable as well, so I want to avoid bad habits.

View Replies View Related

JAVA Calculator - Equals Method To Return Appropriate Value Using Standard Arithmetic

Apr 18, 2014

I am having trouble using the equals method to return the appropriate value using standard arithmetic.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

[Code] ....

I know that "inText.setText(Integer.toString(secondOperand));" in the code is wrong, I just don't know the right code...

View Replies View Related

Program That Implement Final Method And Perform Arithmetic Operation

Mar 4, 2014

class A
{
public final int Add(int a,int b) {
return a+b;
}
}

class B
{
public static void main (String args[])
{
System.out.println(Model.Add(5,10));
}
}

This is what I have. I'm not sure if this even makes use of a final method. But when I run it I get an error saying cannot find symbol for line 13.

View Replies View Related

Post Integer Variables In Next Class

Jan 31, 2014

I write a small program. I want post my integer variables in the next class. Here a code(I wanna change answer color):

Main class(pagrindine.java):
import java.awt.*;
import javax.swing.*;
import java.awt.event.*; 
import javax.swing.event.*;
//Mokinio rinkinukas su skaiciofke kur gali keisti atsakymo spalva, pabandyti sukurti uzrasu knygute.
 
[code]....

So, I want to post variables r,g,b from class slideris.java in the pagrindiness.java.

View Replies View Related

Integer Variables - Which Three Logical Expressions Are Equivalent To Each Other

Apr 12, 2014

Assuming that x, y, and z are integer variables, which of the following three logical expressions are equivalent to each other, that is, have equal values for all possible values of x, y, and z?
 
(x == y && x != z) || (x != y && x == z)
(x == y || x == z) && (x != y || x != z)
(x == y) != (x == z)
 
None of the three
 
A. I and II only 
B. II and III only 
C. I and III only
  D. I, II, and III

I selected B, but got it wrong. I really think I need understanding boolean logic. The correct answer says something else but I don't get the logic. Here is the correct answer:

Answer Key : The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here.

Expression III is the key to the answer: all three expressions state the fact that exactly one out of two equalities, x == y or x == z, is true. Expression I states that either the first and not the second or the second and not the first is true. Expression II states that one of the two is true and one of the two is false. Expression III simply states that they have different values. All three boil down to the same thing. The answer is E.

In exercise 4, I get the same problem:

The expression !((x <= y) && (y > 5)) is equivalent to which of the following?

A. (x <= y) && (y > 5)
B. (x <= y) || (y > 5)
C. (x >= y) || (y < 5)
D. (x > y) || (y <= 5)
E. (x > y) && (y <= 5)

Exercise 4
ABCDE
Incorrect
Score: 0 / 1
Submitted: 2/10/2014 8:21pm
Your answer is incorrect.
Answer Key

The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here. The given expression is pretty long, so if you try to plug in specific numbers you may lose a lot of time. Use De Morgan's Laws instead:

!((x <= y) && (y > 5))
 !(x <= y) || !(y > 5)

When ! is distributed,
&& changes into ||, and vice-versa

(x > y) || (y <= 5)

View Replies View Related

Can't Seem To Get Value For Integer 1 And Integer 2 To Pass With SetValue Method

Aug 10, 2014

public class MyInteger {
private int value;
public MyInteger(int number){
value = number;
System.out.println("Constructor created with value of " + value);

[code]....

I can't seem to get the value for integer1 and integer2 to pass with the setValue method. I get a runtime error stating the I need to have an int value for these two integers.

View Replies View Related

Call Variables From Main To Write To External File

Apr 27, 2015

I have this program, I am wondering if it is possible to call files from the main method and sort them into my saveOneRocord method? If so, how would that look?

my orderProcess Class

package stu.paston.finalprogram;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileWriter;

[Code] .....

View Replies View Related

Accessing Instance Variables And Methods

Oct 24, 2014

I have the following 2 classes:

class Address {
private int a;
public void set_a(int a) {
this.a = a;
}
}
class Person {
private Address address;
}

How do i access the method set_a() (through the "address" in Person) from another class which contains main() ?

View Replies View Related

Variables Over Methods - Unresolved Compilation

Mar 17, 2015

I'm making a java program that can "convert" java to arduino by sending commands over a serial port. I'm using the JSSC library, and using methods to shorten things up a bit.

I've already made the code that starts up the serial port and it does connect. But I've made two different classes for the methods. One for the RXTX and one for the Arduino.

I'm making it so that I can just type:

Java Code:

public static void main(String[] args) {
RXTX.SerialSetup();
Arduino.pinMode(2, 0);
} mh_sh_highlight_all('java');

And it will start the serial port and set pinmode on pin 2 to output.

But when I try it I get this error:

Java Code:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

serialPort cannot be resolved mh_sh_highlight_all('java');

I don't know if its possible to 'send' a variable to a different method.

View Replies View Related

Do Inherited Methods Use Their Instance Variables?

Jan 8, 2014

Do inherited methods use their instance variables or do they use the ones in the method that inherits them?

For example, Class B extends Class A. Class A and B both have the instance variable "potato". A client program tries to use method "cut" using an object of Class B, but class B has no cut method. So, class B uses the "cut" method inherited from class A. What I want to know is will that cut class A's potato or class B's?

View Replies View Related

Modify And Use A GUI To Set Variables Setter Methods

Feb 24, 2012

i'm trying to modify and use a GUI to set the variables the setter methods. while the code seems valid to me and should work perfectly, i get ArrayIndexOutOfBounds and StringIndexOutOfBounds and respectively lines 111 and 134. i'm am not the original author of this code, all i want is to get it to work fine.

1package de.kugihan.dictionaryformids.dictdconv;
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.OutputStreamWriter;
6

[code]....

View Replies View Related

When To Use Multiple Main Methods

Apr 7, 2014

I am confused when it is proper to use an application with multiple entry points, or I guess an application with multiple interconnected modules. I have a network application (Netty) as well as a web application (spring). I can bundle them together, in effect tightly coupling them together, or I can modularize them to operate interdependently of each other while still working together to make the application whole.

Is there any specific reason for making an application a single entity vs multiple entities? Is it "desired" to have a self contained application (eg. One main method)?

View Replies View Related

Int To Set Integer - No Direct Methods

May 12, 2014

I have a method that returns an int[]. I need to check if this int array ( unsorted ) contains a few numbers. I guess the Collection API( Java 1.6) does not have anything like Arrays.asList(<the array>) that can convert an int[] to a Set<Integer> or to a List<Integer> or to even an Integer[]. Or would it?

If not, then the only way I have is to scan the array element by element and then check if it has those numbers. Right?

View Replies View Related

Using Setter And Getter Methods Instead Of Altering Variables

Apr 15, 2015

i need to change my code in order to stop the member variables from being directly altered and its been suggested that i should use a setter and getter method. Ive read up about these and im still unsure at how they should be implemented into my code for my project.

import ddf.minim.*;
DragShape[] piece =new DragShape[77];
PImage puz16;
PImage frame;
PImage text;

[code]....

View Replies View Related

Object / Methods - Private Instance Variables

Jun 30, 2014

so, i was reading my java book and learning about objects and methods and it starts talking about Encapsulation and mentions that it's good practice to set instance variables as private and instead of accessing the instance variables directly, we should create a set method and get method to get and set the stuff we want to pass to the class containing the object...

for example, in this class, we're passing the integer 70 for object dog one and integer 8 for object dog two for the dog class... and these these 2 integers are sent to the setsize method so we're not accessing instance variable size directly.

i dont quite get it though....if we the programmer are the one deciding what size the integer is for the dog, and the setsize method takes the one.setSize(70) or (8) and puts them in setsize(int s) as s... but only to copy that integer stored in s back to private int size.... why do we even need to bother with making these two extra methods such as setSize, getSize?

in the book it says that... well what if the code gets into the wrong hand and someone writes something like one.setSize(0) then you would get a dog with size 0 which is essentially illogical. but then again, i'm the programmer, and i am the person who writes the code and passing the right integer.The reason for public and private... that part i understand... i can see why if a variable's data can get changed amidst the code during calculations and you dont want it to directly change the original variable and have it mess up the code, but this code from the book just a bad example of demonstrating the reason? since we manually pass the information ourselves and passing it to method setSize... and all setSize does is stores it in another integer, only to copy it right away to size (which is the original private variable we were tryign to protect?

Any simple code to demonstrate how the code might end up changing an instance variable and why we would want to protect it by using private?

class GoodDog {
private int size;
public int getSize() {
return size;
}
public void setSize(int s) {
size = s;

[code]...

View Replies View Related

JSF :: Call Variables Instead Of Methods From A Xhtml Page?

Jun 2, 2014

If I had this code:

<h:dataTable value="#{customer.customerList}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>

And in the server getCustomerList() accessed to database, how many times getCustomerList() would be called from I request the xhtml page?. I have read this would be called several times because of JSF internals and It would be better to store it in a variable and access this variable.

1. Is this true this would be called several times? why?

2. If the previous statement was true, how to avoid it, I mean not call the method from a service?

View Replies View Related

Return A String That Holds User Inputs

Apr 18, 2015

I'm working on an assignment for my Java Programming class that requires me to work with Multiple Classes.

Overview of the assignment: Create a "Database" that allows a user to input information about a College Class.

My program has three Classes:

The information that must be tracked in the Course Class is:
- Section Number
- Instructor
- Classroom
- Students (an ArrayList of class Student)

The information that must be tracked in the Student Class is:
- Name
- Major
- GPA
- Hours

The Driver class will use the main method to print the user a prompt asking for inputs, then display all of the user's inputs after he/she wished to exit the program.

I'm having no trouble with asking the user for inputs and then checking the validations for the inputs, I'm currently struggling with return a String that houses all of the user's inputs for the variables above. The information must be returned in a method called toString(). Both the Student and Course Class must have a toString() method that returns the information in the class (value of the variables), I must then call upon these methods from the main method of the Driver Class to print them back to the user.

The toString methods within the Student & Course Classes are at the bottom of the code

To Summarize: What I need to do within the toString() methods to return the values the user has inputted for the various variables? Such as.. Should I pass something through the method? Do I need the this keyboard to pass through the variable again? I just fear that I'm currently misunderstanding a very critical part of programming.

My Driver Class

import java.util.Scanner;
public class Driver {
Course courseInputs = new Course();
Student studentInputs = new Student();
public static void main(String[] args) {

[Code] .....

View Replies View Related

Calling Child Methods From Main

May 7, 2015

I can call a child method from a main method. If I have a parent called "Assessment", a child called "Quiz", and another child called "test". Can I instantiate an object like Assessment a = new test(); and call a method in test.

I know the method can be called if I instantiate the test t = new test() but that defeats the purpose of inheritance...

View Replies View Related

Instance Variables Passed In As Arguments To Static Methods?

Feb 19, 2011

I thought static methods could never use instance variables, because they wouldn't know which instance to look at.

From Head First Java, p. 284: "A static method is not associated with a particular instance - only the class - so it cannot access any instance variable values of its class. It wouldn't know which instance's values to use."

Now I was answering some mock exam questions from Cameron McKenzie's SCJA book, and I don't understand one of the options. On page 205, the last question has an option that says: "Instance variables ... are not visible in static methods, unless passed in as arguments." This option is supposed to be correct. Now... how does that work?

View Replies View Related

Java Swing Frame Can't Call On Other Methods From Main

Mar 25, 2015

I am trying to make a 2d graphical animation using the java swing classes in order to make a frame. I had a basic version working when all of the code was under the main method, but when I moved some into another method it broke it. With y understanding of java my code should work as I create a variable of the method containing the code and then assign the size and exit button. However this causes many problems such as my BeaconFrame method informing me that it is unused when I have used it. Here is my code:

import javax.swing.*;
import java.awt.*;
 public class BelishaBeacon {
  public void BeaconFrame() {
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();

[code]....

View Replies View Related

How To Return Array From A Method / Back Into Main Method That Prints Out Stuff

May 27, 2014

I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?

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

How To Call A Method That Exist Within A Class Into Main Method

Feb 13, 2014

I am just trying to test this array, for a locker combination program that involves classes...but the array is printing out the whacky numbers for the location. When I try to call the method in the main, it does not work. How do I call a method that exist within a class into the main method?

public class locker { 
public static void main(String[] args) {
CombinationLock();

[code]....

View Replies View Related

Unable To Call / Test All Of The 5 Sorting Methods At Same Time In Main Class

Dec 19, 2014

For reference I am programming Java in BlueJ. I am fairly new to the language and I am having trouble with sorting.

I am trying to call / test all of the 5 sorting methods (at the same time) in the main class. To be specific, the sorted list has to technically outputted 5 times.

I figured out how to call / test Quicksort:

Sorting.quickSort(friends, 0, friends.length-1);

But the others are not working correctly. Specifically these:

Sorting.mergeSort(friends, 0, friends.length-1);
Sorting.PbubbleSort(friends, 0, friends.length-1);
Sorting.PinsertionSort(friends, 0, friends.length-1);
Sorting.selectionSort(friends, 0, friends.length-1);

For reference, this is the output when it is not sorted:

Smith, John 610-555-7384
Barnes, Sarah215-555-3827
Riley, Mark 733-555-2969
Getz, Laura 663-555-3984
Smith, Larry464-555-3489
Phelps, Frank322-555-2284
Grant, Marsha243-555-2837

This is the output when it is sorted:

Barnes, Sarah215-555-3827
Getz, Laura 663-555-3984
Grant, Marsha243-555-2837
Phelps, Frank322-555-2284
Riley, Mark 733-555-2969
Smith, John 610-555-7384
Smith, Larry464-555-3489

This is the class Sorting, which I should note is all correct:

public class Sorting{
/**
* Swaps to elements in an array. Used by various sorting algorithms.
*
* @param data the array in which the elements are swapped
* @param index1 the index of the first element to be swapped
* @param index2 the index of the second element to be swapped
*/
private static <T extends Comparable<? super T>> void swap(T[] data,
int index1, int index2){
T temp = data[index1];
data[index1] = data[index2];

[Code]...

This is the Main class in which I am supposed to call the sorting methods, SortPhoneList:

public class SortPhoneList{
/**
* Creates an array of Contact objects, sorts them, then prints
* them.
*/
public static void main (String[] args){
Contact[] friends = new Contact[7];
friends[0] = new Contact ("John", "Smith", "610-555-7384");
friends[1] = new Contact ("Sarah", "Barnes", "215-555-3827");

[Code]...

View Replies View Related

No Main Methods / Applets / Or MIDlets Found In File Error Message

Apr 17, 2015

I am assigned to create a program "Simpletron" that the only language understands it is Simpletron Machine Language or SML. I figured out the most but I get the message "No main methods, applets, or MIDlets found in file" when compiling the program. I pasted my program so that you can see.

//A Simpletron computer simulator */
import javax.swing.*;
import java.text.DecimalFormat;
import java.awt.*;
import java.awt.event.*;

[code]....

View Replies View Related







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