Java Programming: Variables, Data Types, and Operators - Exam 1 CS-1440 Study Guide, Exams of Nursing

A comprehensive overview of fundamental java programming concepts, including variables, data types, operators, and assignment operators. It explains the difference between variables and literals, how to declare and initialize variables, and the purpose of data types. The document also covers arithmetic operators, operator precedence, and combined assignment operators. It is a valuable resource for students preparing for exam 1 in cs-1440 java programming.

Typology: Exams

2024/2025

Available from 03/26/2025

Lectsmith
Lectsmith 🇺🇸

2.6

(5)

2.2K documents

1 / 76

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Exam 1 CS-1440 Java Programming
Intro
- The difference between a variable and a literal - A literal is notation for representing a fixed ( const )
value. A variable is storage location associated with a symbolic name (pointed to, if you'd like). In any
programming language a Literal is a constant value, where as identifiers can change their values.
Identifiers can store literals and process them further.
How to declare a variable - To declare (create) a variable, you will specify the type, leave at least one
space, then the name for the variable and end the line with a semicolon ( ; ). Java uses the keyword int
for integer, double for a floating point number (a double precision number), and boolean for a Boolean
value (true or false).
int score;
How to assign a value to a variable. - Java is pass-by-value. That means pass-by-copy
Declare an int variable and assign it the value '7'. The bit pattern for 7 goes into the variable named x.
int x = 7;
How to initialize a variable. - Initializing a variable. Initializing a variable means specifying an initial value
to assign to it (i.e., before it is used at all). Notice that a variable that is not initialized does not have a
defined value, hence it cannot be used until it is assigned such a value.
- What a data type is (i.e., what makes up a data type) - A data type is a set of values and a set of
operations defined on them. For example, we are familiar with numbers and with operations defined on
them such as addition and multiplication. There are eight different built-in types of data in Java, mostly
different kinds of numbers. We use the system type for strings of characters so frequently that we also
consider it here.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c

Partial preview of the text

Download Java Programming: Variables, Data Types, and Operators - Exam 1 CS-1440 Study Guide and more Exams Nursing in PDF only on Docsity!

Exam 1 CS-1440 Java Programming

Intro

  • The difference between a variable and a literal - A literal is notation for representing a fixed ( const ) value. A variable is storage location associated with a symbolic name (pointed to, if you'd like). In any programming language a Literal is a constant value, where as identifiers can change their values. Identifiers can store literals and process them further. How to declare a variable - To declare (create) a variable, you will specify the type, leave at least one space, then the name for the variable and end the line with a semicolon ( ; ). Java uses the keyword int for integer, double for a floating point number (a double precision number), and boolean for a Boolean value (true or false). int score; How to assign a value to a variable. - Java is pass-by-value. That means pass-by-copy Declare an int variable and assign it the value '7'. The bit pattern for 7 goes into the variable named x. int x = 7; How to initialize a variable. - Initializing a variable. Initializing a variable means specifying an initial value to assign to it (i.e., before it is used at all). Notice that a variable that is not initialized does not have a defined value, hence it cannot be used until it is assigned such a value.
  • What a data type is (i.e., what makes up a data type) - A data type is a set of values and a set of operations defined on them. For example, we are familiar with numbers and with operations defined on them such as addition and multiplication. There are eight different built-in types of data in Java, mostly different kinds of numbers. We use the system type for strings of characters so frequently that we also consider it here.

The eight primitive data types, and which ones we are using in this course - Variables and the 8 Primitive Data Types The Purpose of a Variable (and some vocabulary) You can think of a simple program as a list of instructions to be read and acted upon sequentially Example: Read a value representing the radius of a circle from the standard input source/stream Compute the area of a circle with this radius Print the area to the standard output stream (i.e., the console window) Remember: a computer will read and act upon these instructions one at a time - it is not aware of what is coming up until it gets there! Looking at step 1 in the program above, we will need to tell the computer that it needs to remember the value it is reading in - it needs to store this value in its memory somewhere so we can use it in a computation later. We need to tell the computer how much memory will be needed to store the value in question. Different kinds of numbers require different amounts of memory (more on this in a minute). Of course, we sometimes need to store things other than numbers. These things too, come in different sizes. For example, it will certainly take more memory to store the Declaration of Independence than it will to store a single letter (i.e., a "character"). Further, we also need to tell the computer how the value should be stored in memory (i.e., what method of "encoding" should be employed to turn the value into a string of 1's and 0's). Examples of types of encodings used include Two's Complement, IEEE 754 Form, ASCII, Unicode, etc... The computer also needs to have some reference to where it stored the value, so it can find it again. The concept of a variable solves all of our problems here. A variable in Java gives us a way to store values (or other kinds of information) for later use, addressing all of the aforementioned considerations.

System.out.println(" i = " + i); System.out.println(" j = " + j); System.out.println(" x = " + x); System.out.println(" y = " + y); //adding numbers System.out.println("Adding..."); System.out.println(" i + j = " + (i + j)); System.out.println(" x + y = " + (x + y)); //subtracting numbers System.out.println("Subtracting..."); System.out.println(" i - j = " + (i - j)); System.out.println(" x - y = " + (x - y)); //multiplying numbers System.out.println("Multiplying..."); System.out.println(" i * j = " + (i * j)); System.out.println(" x * y = " + (x * y)); //dividing numbers System.out.println("Dividing..."); System.out.println(" i / j = " + (i / j)); System.out.println(" x / y = " + (x / y));

//computing the remainder Understand operator precedence and associativity - Associativity is used when two operators of same precedence appear in an expression. Associativity can be either Left to Right or Right to Left. For example '*' and '/' have same precedence and their associativity is Left to Right, so the expression "100 / 10 * 10" is treated as "(100 / 10) * 10". How to use a combined assignment operator - Variables and Combined Assignment Operators You'll commonly want to complete a math function and assign the resulting value back to some named object, called a variable. ActionScript makes this easier by letting you combine arithmetic and assignment operators together. Take a look at an assignment operator example: Create a new ActionScript 3.0 project and enter in the following code for the project: // Assignment Operators var myValue:Number = 2; myValue = myValue + 2; trace(myValue); var myOtherValue:Number = 2; myOtherValue += 2; trace(myOtherValue); Run the project. You'll get the following in the Output panel: 4 4 Let's walk through the code and explain how you get this result and what role variables and combined assignment operators play. Variables

Widening a smaller primitive value to a bigger primitive type. class A { public static void main(String... ar) { byte b=10; short s= b; //byte value is widened to short int i=b; //byte value is widened to int long l=b; //byte value is widened to long float f=b; //byte value is widened to float double d=b; //byte value is widened to double System.out.println("short value : "+ s); System.out.println("int value : "+ i); System.out.println("long value : "+ l); System.out.println("float value : "+ f); System.out.println("double value : "+ d); }

Output- short value : 10 int value : 10 long value : 10 float value : 10. double value : 10. In the preceding code, we have widened a smaller byte value to several bigger primitive values like byte, short, int, long and float. Widening a subclass object reference to a wider superclass object reference. This is also known as upcasting the subclass reference to its superclass reference. class A { public void message() { System.out.println("message from A"); } }

In these situations, you can use a process called casting to convert a value from one type to another. Although casting is reasonably simple, the process is complicated by the fact that Java has both primitive types (such as int, float, and boolean) and object types (String, Point, and the like).

  • How to create a named constant. - Name an array constant Click Formulas > Define Name. In the Name box, enter a name for your constant. In the Refers to box, enter your constant. ... Click OK. In your worksheet, select the cells that will contain your constant. In the formula bar, enter an equal sign and the name of the constant, such as =Quarter1. Press Ctrl+Shift+Enter. What a reference variable is - A reference variable is an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable. How to declare a variable of type String. - Ask Question 2 This question already has an answer here: What are classes, references and objects? 9 answers class Demo {

String title; private int num; } String is a class, so when we declare title, is that treated as object or just a variable? I know this is a very basic thing, but i need help. Thanks in advance. How to read keyboard input from the user. - Input from Keyboard The input Function Input via keyboard There are hardly any programs without any input. Input can come in various ways, for example from a database, another computer, mouse clicks and movements or from the internet. Yet, in most cases the input stems from the keyboard. For this purpose, Python provides the function input(). input has an optional parameter, which is the prompt string. If the input function is called, the program flow will be stopped until the user has given an input and has ended the input with the return key. The text of the optional parameter, i.e. the prompt, will be printed on the screen. The input of the user will be interpreted. If the user e.g. puts in an integer value, the input function returns this integer value. If the user on the other hand inputs a list, the function will return a list. Let's have a look at the following example: name = input("What's your name? ") print("Nice to meet you " + name + "!") age = input("Your age? ") print("So, you are already " + str(age) + " years old, " + name + "!") We save the program as "input_test.py" and run it: $ python input_test.py

Left-justifying printf integer output The printf integer zero-fill option printf integer formatting formatting floating point numbers with printf printf string formatting printf special characters Related printf content Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here. A great thing about the printf formatting syntax is that the format specifiers you can use are very similar — if not identical — between different languages, including C, C++, Java, Perl, PHP, Ruby, Scala, and others. This means that your printf knowledge is reusable, which is a good thing. printf formatting with Perl and Java In this cheat sheet I'll show all the examples using Perl, but at first it might help to see one example using both Perl and Java. Therefore, here's a simple Perl printf example to get things started: printf("the %s jumped over the %s, %d times", "cow", "moon", 2); And here are three different Java printf examples, using different string formatting methods that are available to you in the Java programming language: System.out.format("the %s jumped over the %s, %d times", "cow", "moon", 2); System.err.format("the %s jumped over the %s, %d times", "cow", "moon", 2); String result = String.format("the %s jumped over the %s, %d times", "cow", "moon", 2);

As you can see in that last String.format example, that line of code doesn't print any output, while the first line prints to standard output, and the second line prints to standard error. In the remainder of this document I'll use Perl examples, but ag How to use String.format to nicely format strings. - The most common way of formatting a string in java is using String.format(). If there were a "java sprintf" then this would be it. String output = String.format("%s = %d", "joe", 35); For formatted console output, you can use printf() or the format() method of System.out and System.err PrintStreams. Our style conventions for naming program entities, including comments in our programs, and formatting our programs. - Google Python Style Guide 1 Background Python is the main dynamic language used at Google. This style guide is a list of dos and don'ts for Python programs. To help you format code correctly, we've created a settings file for Vim. For Emacs, the default settings should be fine. Many teams use the yapf auto-formatter to avoid arguing over formatting. 2 Python Language Rules 2.1 Lint Run pylint over your code.

Suppressing in this way has the advantage that we can easily search for suppressions and revisit them. You can get a list of pylint warnings by doing: pylint --list-msgs To get more information on a particular message, use: pylint --help-msg=C Prefer pylint: disable to the deprecated older form pylint: disable-msg. Unused argument warnings can be suppressed by deleting the variables at the What a class definition is and how to write one - How to define class in Python? The key concept in this programming paradigm is classes. In Python, these are used to create objects which can have attributes. Objects are specific instances of a class. A class is essentially a blueprint of what an object is and how it should behave. How a class can be considered a "smart data type" - Rational Software Architect 8.5.x: new features and enhancements Be the first to ask a question Product documentation Abstract This document provides an overview of new features and enhancments in IBM Rational Software Architect version 8.5 releases.

Content Tab navigation 9.0 Release 8.5.5 Fix Pack 4 8.5.5 Fix Pack 3 8.5.5 Fix Pack 2 8.5.5 Fix Pack 1 8.5 Fix Pack 5 8.5 Mod Pack 1 8.5. Release Rational Software Architect, Version 8.5.5. Link Date Released Status Download 8.5.5.4 Release 11/20/2015 Current This release of Rational Software Architect contains new features and enhancements in the following areas: IBM product integration support IBM Runtime Environment Java Technology Edition updates Eclipse platform updates Product integration support The following product integration is supported beginning with v8.5.5.4: Rational Common Licensing 8.1.4. IBM Installation Manager 1.8. Rational Team Concert 5.0.2 and 6. Back to top IBM Runtime Environment Java Technology Edition updates IBM Runtime Environment Java Technology Edition is updated to the following versions: IBM 32/64-bit Runtime Environment for Windows, Java Technology Edition, Version 7.0 Service Release 9 FP IBM 32/64-bit Runtime Environment for Linux, Java Technology Edition, Version 7.0 Service Release 9 FP

Public access-modifiers-in-java Default: When no access modifier is specified for a class , method or data member - It is said to be having the default access modifier by default. The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package. In this example, we will create two packages and the classes in the packages will be having the default access modifiers and we will try to access a class from one package from a class of second package. filter_none edit play_arrow brightness_ //Java program to illustrate default modifier package p1; //Class Geeks is having Default access modifier class Geek { void display() { System.out.println("Hello World!"); } } filter_none

edit play_arrow brightness_ //Java program to illustrate error while //using class from different package with //default modifier package p2; import p1.*; //This class is having default access modifier class GeekNew { public static void main(String args[]) { //accessing class Geek from package p Geeks obj = new Geek(); obj.display(); } } Output: