Share
Explore BrainMass

Fundamentals of Numeric Variables

How do you figure out how to declare a variable? How do you know what type of variable to use? What is the best way to name a variable and why? What are the differences between an integer variable and a floating point variable? When and why do data type conversions occur? What is the modulus operator?

Write a short java statement for each question item

1) Declare a variable for each of the data types, pay attention to the naming convention and syntax

a. a int variable representing the number of students, the value of this variable is 45.

b. a double variable representing a student' s GPA.

c. a float variable representing the amount you need to pay for gas at a gas station.

d. a char variable that has the value of C.

e. a String variable containing the name of your pet/hometown/your favorite dish.

2) Write down the result for the following statement.

a. inta=15; float b = 15.0; ?c = a + b;?Question: the data type of c should be ___30___

b. inta=5;?double b = 15.0;?c = a + b;?Question: the value of c is ___20___, the data type of c is ___double____

c. int numOfPeople = 10;?int numOfApple = 4;?c = numOfPeople / numOfApple;?Question: the value of c is __2.5_____, the data type of c should be _______

d. int numOfPeople = 10;?int numOfApple = 4;?c = numOfPeople % numOfApple;?Question: the value of c is _______, the data type of c should be _______

e. int numOfPeople =10;?int numOfApple = 4;?double applePerPerson = 0.0;?applePerPerson = numOfPeople / numOfApple; Question: the value of applePerPerson is ______.

f. int num1 = 5; int num2 = 4; ?int result = num1 % numb2;?Question: the value of result is __________.

g. Let assume you have 1003 cents, using / and % to write a block of java statement to calculate the number of dollars, quarters, and cents you have.
Section Three

Write a block of pseudo code that captures the following federal tax bracket, please note that the following chart is an approximation of the tax bracket, but is not the actual tax bracket:

For people who file single status, Taxable income
0 - $8,500 $8500-$34,500 $34,500 - $83,600 $83,600 - $174,400 $174,400 - $379,150
$379,150 above
tax bracket 10%?15%?25%
28% 33%
35%

Attachments

Solution Preview

Some of the goals in Beginning Computer Science homework are to teach you: How to write program code that a compiler or interpreter will be find syntactically correct; What particular data types there are and how they are best used; What particular data operators there are and how to use them; And how to make your program code easily understood by your fellow humans, who might have a need to work on code you've written. This document will help you with homework and assignments where you are learning to declare and use variables, in particular with numeric types of variables.

When declaring variables the specific syntax, or the exact things that you have to type in, depends on the particular language you are writing code for. This document is based on the specific syntax used in Java, but the information is true of almost all computer languages. What is necessary, regardless of what language you are using, is to look up the exact way the language needs you to type it into the file that you will ultimately run through the programming language's interpreter or compiler. Java, for example, has both an interpreter and a compiler. The C programming language only uses a compiler. The LISP language, one of the fundamental languages used in artificial intelligence applications, uses an interpreter. But regardless of the language, or if it's an interpreter or a compiler, there are specific things that you have to type in. If you have access to whatever the particular language you are doing homework for, you can even use that to make sure your answers are correct, and that, for example, you haven't left off a semicolon.

The basic types of variables are: Character strings (like "apple", or "John Doe"), numbers, and logicals (a variable that hold the value of either true or false). In the computer world, numbers are typically divided into two types, depending on whether there is a decimal point. A number that does not have a decimal point, is called an integer. That name comes from the formal definition in mathematics. Examples of integers are: 1, 2, 3, 4, -1, -2. -4, and -104. If a number has a decimal point, it's referred to as a "real" number, or in the world of Computer Science, a "floating point" number. Examples of these are: 1.1, 2.345, 0.324, -2.334, and -100.45.

A distinction has to be made between those two types of numbers when creating a computer language because of the way you need to store the numbers and to manipulate them (add, subtract, or multiply them, for example). And therefore, the programmer has to decide what kinds of numbers he or she will be working with so that they can declare the variables accordingly. A GPA (grade point average) typically looks like this: 3.764, and as such it is a real number, as opposed to an integer. The age of a person is typically expressed as an integer. You speak of a person being 18 years old, or 20 years old, not 18.5 (eighteen and a half) years old or 20.25 (twenty and a quarter) years old.

Beginning Computer Science homework will often ask questions like this:

Declare an int variable representing the number of students, that has the value of 45. Pay attention to the naming convention and syntax.

You'll need to look up the exact way that the computer language you are studying wants you type it in. That's a simple statement, but it's ...

Solution Summary

This document, containing over 2,000 words, will help you with homework and assignments about learning to declare and use variables, in particular with the declaration and use of numeric types of variables. It explains how to figure out the correct way to declare variables in a computer programming language, the differences between an integer and a floating
point number, and explains the (sometimes counter intuitive) ways arithmetic operators work, including the modulus operator.

$2.19