Compound Assignment Operator

Jan 8, 2014

I just cant seem to understand the order of precedence here.

class Test{    
public static void main(String[] args){       
int k = 1;             
k += (k = 4) * (k + 2);       
System.out.println( k );    
}
}

From what I have read compound operators have the lowest order of precedence... But the above piece of code makes the assignment k = 1 + (k = 4) * (k + 2) before evaluating the rest of the statement first.

It then evaluates (k = 4) and proceeds with the remained of the statement 1 + 4 * (4 + 6)....

I dont understand why the first k is assigned 1 but the remaining ks 4. Should they not all be 1 or 4 (I would have thought 4, since += has the lost order of precedence so is evaluated last)??

View Replies


ADVERTISEMENT

How To Get A Compound Interest Program To Work

Apr 10, 2014

/code

package com.eclipsedistilled.cis125;
import java.util.Scanner;
public class HowellBank {
public static void main(String[] args) {
//Written by George Torgerson
//Chapter 4 Link to Logic 7
//Program that accepts an account number, the account owner's first
//Declarations
int accountNumber;

[code]....

This is for college homework. Fictional bank. I would like the year to look like Year 1, Year 2, etc.

View Replies View Related

Generating Compound Interest With Integers

Sep 15, 2011

I am trying to change this code to use only integers to calculate the compound interest.

// Compound-interest calculations with for.

public class Interest {
public static void main( String args[] ) {
double amount; // amount on deposit at end of each year
double principal = 1000.0; // initial amount before interest
double rate = 0.05; // interest rate

[Code] .....

And here is the output I get :

Why do I get the output after year 2? I assume it has something to do with the remainder.

I also have to format this output with the decimal point, etc.. which I think I will be ok with after I get through this part.

View Replies View Related

Create Compound Shapes / Paths In JavaFX?

Apr 28, 2015

Is there some way to create compound shapes/paths in JavaFX?

For the record I'm not implying the use of the methods intersect, subtract, or union. These produce other shapes. A compound shape/path is one that has a knockout of some sort. For instance, a circle within a circle, such as in a 2d donut shape. Alternatives do not include a circle with a thick stroke nor an overlayed circle with the background color. Specifically, JavaFX supports the FillRule, in the case of the Path object. However, there doesn't appear to be an "add" method as there was in the Area shape in Swing.

View Replies View Related

Creating A Program That Calculates Simple And Compound Interest

Aug 12, 2014

I need getting started designing and creating a program that calculates simple and compound interest. Simple interest in this case means that interest is only applied to the original amount. For instance, if a person deposits $1000 at 10% annual interest, then after one year they would have $1100 (the original $1000 plus the $100 interest) and after two years they would have $1200 (the original $1000, plus the $100 earned the first year, plus the $100 earned the second year).

With compound interest, the interest is applied to all money earned. So, starting with $1000 at 10% annual interest, after one year the user would have $1100 (the original amount plus $100 interest) and after two years the user would have $1210 (the $1100 they started with at the beginning of the year plus the $110 interest).

The requirements of this program are:

-Create a class that will hold methods to calculate the interest. Do not include the main method in this class.
-In the class that is used to calculate the interest, include a method to print to the screen, by interest period, the starting amount for the period, the interest earned for the period, and the total amount at the end of the period. Note that each period represents the time frame for how often it is updated. If annual is selected the period is every year. If it is semi-annual it is every six months. If it is quarterly it is every three months.
-Create a demo class that will use your interest calculation class. Prompt the user for the original amount, the type of interest (simple or compound), the annual interest rate, and whether it is updated annually, semi-annually, or quarterly. Use method calls to your calculate interest class to do all calculations and display the result. Loop the program until the user chooses to quit.

Sample output ($10000 initial investment, 10% simple interest, annual, for 4 years):

Period Beginning Amount Interest Earned Ending Amount
1 10000.00 1000.00 11000.00
2 11000.00 1000.00 12000.00
3 12000.00 1000.00 13000.00
4 13000.00 1000.00 14000.00

View Replies View Related

Amortization Algorithm - Loan Calculator That Uses Compound Interest

May 21, 2014

I'm making a loan calculator that uses compound interest... I need the program to return the monthly payment, how much of it is principal and how much of it is interest, which depends on how many months into your loan you are... So basically all I need is the algorithm to calculate how much of your monthly payment (depending on how many months in you are) is interest and how much of it is principal.

View Replies View Related

Using MATH Class For Assignment?

Sep 18, 2014

Here is the assignment:

Prompt the user to input two sides (a and b) of a right triangle. Calculate, using the

formulas below, the hypotenuse and the other two angles of the triangle. Calculate the

perimeter and the area.

Here are the formulas:

a^2 + b^2 = h^2

sinθ = a/h

A + B + C = 180 degrees

how to start this program out, i know which math classes i need to use.

View Replies View Related

Two Dimensional Array Assignment

Jun 14, 2014

I am working on the following java assignment..Write a program that randomly fills in 0s and 1s into a 4- by- 4 matrix, prints the matrix, and finds the first row and column with the most 1s. Here is a sample run of the program:

0011
0011
1101
1010

The largest row index: 2
The largest column index: 2

I have code that generates random 0s and 1s for the array, how to get the largest column and row.

import java.util.Random;
public class LargestRowColumn {
public static void main(String[] args){
//create 4x4 array matrix
int arrayMatrix[][] = new int[4][4];

[code]....

finding the row and column with the largest amount of 1s. I keep thinking well if I scan and find a one in the array, maybe I can just save the index of the row and column and then determine which index contains the most 1's after the array has been scanned.

View Replies View Related

Accessing Toolkit For Assignment

Apr 2, 2014

I'm trying to access the toolkit for an assignment I'm writing and I keep getting the same compiler message. What am I doing wrong?

import java.text.DecimalFormat;
import java.io.*;
import java.util.Scanner;
public class Robert_Gardner_6_07
{
public static void main(String[] args) throws IOException

[code]....

View Replies View Related

Instantiating Object Without Assignment

May 17, 2014

I would like to know what is the significance of instantiating an object without an assignment.

I have created a class TestClass1 with a single constructor that prints a test message.

In TestClass2, if I write "new TestClass1()" rather than "TestClass1 x = new TestClass1()" it still works, and prints the test message.

I would like to know if I do not assign an object at the time of instantiation, it cannot be referenced or reused later, then what is the significance of this type of construct and when it can be useful, and where is the object being held.
 
public class TestClass1 {
   TestClass1() {
     System.out.println("This is a test message");
   }

public class TestClass2 {

[Code] ....

View Replies View Related

Trying To Get Data From Previous Assignment To Be Displayed In New One Using GUI

Apr 26, 2014

I am trying to get items to display that would display in a command prompt now into a GUI. I am freaking lost at the moment, probably because I've been staring at this code for over a week now. I have included all the files that are necessary to run the program as an attachment for your own testing purposes. Should I be using a TextField to display the data from the CSV files? How do I get the data to be displayed? How would I get it to be displayed based on the different files Staples (newSTPL.csv), Apple (newAPPL.csv), and Microsoft (newMSFT.csv)?

Java Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* DataAnalyzer Class
* This class instantiates the methods from the ReadFiles Class and Calculations Class.

[code]....

View Replies View Related

2 Dimensional Array Assignment With A Loop

May 5, 2015

I have a 2 dimensional array assignment with a loop, i'm supposed find the average score of each student from a grade record and find the average score for each test. I've been trying to do the assignment all day with no progress. This is what i have so far

public class SiuTest {
public static void main(String[] args) {
String [] stu= new String [5];
int [] [] grade= new int [4][2];
String hold;

[code]....

View Replies View Related

Assignment To Create A Sphere Class

May 8, 2015

I have an assignment to create a Sphere class that will allow you to create Sphere objects using the code below. Then create a program called SphereTester that prompts the user for the radii of two spheres in meters. The program should display which sphere is larger and by how many cubic meters and display only four digits after the decimal point. I have the sphere class given to us for the assignment which is this:

public class Sphere {
// instance variable (i.e., a field)
private double radius;
// constructor for the Sphere class
public Sphere(double r) {
radius = r;

[code]....

Here is a sample run of what the final result should look like

Enter the radius of a sphere (in meters): 1
Enter the radius of a 2nd sphere (in meters): 2
Sphere 2 is greater than Sphere 1 by 29.3215 cubic meters

View Replies View Related

Average Score Assignment Using Netbeans

Sep 27, 2014

I've been working on this same assignment using netbeans. I've completed the exercise and when I run it, it works. However, netbeans still had errors listed in the left hand numerical column and it asks me if I still want to run the program with errors when I hit run?

package TestScores;
import java.util.Scanner;
// Name: Joe
// Date: ........
// Desc: Test Score Averages
 
[code].....

I found this thread while doing a google search trying to find out the error in my code.

View Replies View Related

Java Literal And Assignment To Different Primary Types

Jan 20, 2015

I thought numeric literal were by default int or doubles, depending on if have a . and numbers after the But I wrote a quick test program as listed below. I understand the float float floatA = 5.5; failed to compile since 5.5 is a literal of type double and you are trying to assign this to a floag

What I am having problems with is byte byteA = 5; 5 is a literal of type int and this is being assigned to a byte and compiler should complain.The compiler does not allow two byte values to be added and assigned to a byte since the result of the addition is an int

class literalTesting{
public static void main(String[] arg){
byte byteA = 5; // allowed WHY I thought literal is an int and assigning int to byte
byte byteB = 10; // allowed

[code]...

View Replies View Related

Stack Assignment - Give More Than One Option To Input

Sep 10, 2014

I understand the stack part of it, just not the loop, specifically how you give the user more than one option to input.

Implement a printer driver that handles incoming print jobs. The driver will behave in the following manner:

- Loop giving the user the following options: ADD print job, DELETE latest print job, and
QUIT
- Recording the following information from each job: computer name, document name, and
number of pages
- When the user chooses to ADD, add a new print job to the top of the stack
- When the user chooses to DELETE, pop off the latest print job and output to the screen
which job has just been deleted
- When the user chooses to QUIT, output the print jobs still in the stack (in order from top
to bottom)
- Output must include all relevant information for each job.

View Replies View Related

How Curly Braces Follow Assignment Statement

Apr 7, 2015

@Override
@SuppressWarnings("unchecked")
public TableCell<S, T> call(TableColumn<S, T> p) {
TableCell<S, T> cell = new TableCell<S, T>() {
@Override
public void updateItem(Object item, boolean empty) {
if (item == getItem()) {
return;
}

The line in the code above TableCell<S, T> cell = new TableCell<S, T>() {

I don't understand how curly braces follow an assignment statement?What does the line of code mean?

View Replies View Related

How To Write A Client Class To Test Methods For Assignment

Jan 20, 2014

Write a class encapsualting the concept of a course grade, assuming a course grade has the following attributes: a course name and a letter grade. Include a constructor, the accessor and mutator, and methods toString and equals.Write a client class to test all the methods in your class.

package labmodule7num57;
import java.util.*;
public class LabModule7Num57 {
// Constructors//

[code]....

View Replies View Related

Difference Between Operator And Method

Jun 4, 2014

I know instanceof is an operator and println is a method. But what is the difference between the two? How are they different/same?

View Replies View Related

Operator In Case Of Strings

May 23, 2015

After the executing the code below:-

class Test
{
public static void main(String [] args)
{
String s = new String("new") ;
String d = new String("new") ;
System.out.println((s==d)+ " "+s.equals(d)) ;
System.out.println(s==d + " "+ s.equals(d)) ;
}
}
OUTPUT:- false true
false

Why did the output change in the second print statement?

View Replies View Related

Java Assignment To Get User Input And Display ZIP Codes And Populations

Oct 7, 2014

URL....So the problem is that when I type in "PA" it will display about 24 Zips and Populations before it stops. The problem is in the ZIPs file. It goes down the list and then takes the Zip from the Zips file to the Zips in the Population file and displays the Population. It will go to population 513 and stop. Reason being, there is no ZIP code in the Population file to display a population. The loop then stops. How can I get the program to skip over the zip code when there is no corresponding ZIP code in the other file and continue showing the other Pops..Here's what I currently have completed:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Population {
//Declaring global variables.
Scanner fileScannerZip, fileScannerPop, inputFile;
private String lineZip, linePop;
int invalidZip;

[code]...

View Replies View Related

Why Operator Overloading Is Not Supported In Java

Oct 18, 2014

why operator overloading is not supported in Java.

View Replies View Related

Using Values That Are Set For Cases In The Switch Operator

Mar 23, 2014

I am new to Java and I am trying to use values that are set for cases in the switch operator.

The first menu ask for you to pick a product and each product has a price on it.

The second menu ask you to pick a state with each state having a decimal value on it.

The third is asking you to put the number of cases and each case is 12 items.

A key note to remember is that each part that a person is choosing is on a different instance!

Here is an example of what i am trying to do.

Menu 1: I picked case 1 that is Computer and it is worth 1000
Menu 2: I picked case 1 that is CT and it's tax is 7.5

Third choice: I picked case 1 and that has 12 items

I want the subtotal witch is: (1000 * 12)

Subtotal in this situation is: 120000

Next i need the total value which is based on what state they picked for the tax percent value picked from the state menu case: (12000 * 0.075 + 120000)

Total value is: 129000

I will post the code I have but based on the choices a person makes will determine the values and I need those values set in the cases to put in a math equation. The problem I am having is retrieving these numbers form the cases inside the menu options and they are on a different instance. So How can I do this in Java code?

Here is the code:

This is menu 1

Java Code:

import java.util.*; //scanners and more
class menu{
public void display_menu() {
System.out.println ("Please select your product"); //Gives user direction
System.out.println ( "1)

[Code] .....

View Replies View Related

How To Type Bitwise Operator Symbol

Jul 6, 2014

It looks like an upside down v , i cant find it on my keyboard. I assume its a keyboard combo.

View Replies View Related

Program Ask For Operator And Two Numbers And Do Math

Feb 17, 2015

This program is supposed to ask for the operator (+ or -) then ask you for two numbers and do the math. When I run it it comes up- Enter operator. When I say add or sub nothing happens. Here it is:

import java.util.Scanner;
public class Echo1{
public static void main(String args[]){
Scanner userInput = new Scanner(System.in);
System.out.println("Enter Operator");
String operator = userInput.next();

[Code] ....

View Replies View Related

HashMaps / ArrayLists - Diamond Operator?

Jul 8, 2014

So I am learning HashMaps/Arraylists and I can't really understand the diamond operator or what it's for. Take the following code for example: Why could we not just do this without the diamond?

Java Code:

import java.util.HashMap;
class Untitled {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();

map.put("California","Sacramento");
map.put("Oregon","Salem");
map.put("Washington","Olympia");

System.out.println(map);
}
} mh_sh_highlight_all('java');

View Replies View Related







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