Preview

Pt1420 Unit 3 Activity 1 Summary

Powerful Essays
Open Document
Open Document
2338 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Pt1420 Unit 3 Activity 1 Summary
Statement Purpose:
The purpose of this Lab. is to familiarize student how to solve practical problems programmatically; they will practice on elementary programming using primitive data types, variables, constants, operators, expressions, and input and output. Also, they will learn how to diagnose errors that may occur when a program is compiled or executed. There are some exercises, through which they will understand the concept learn in this chapter.

Activity Outcomes:
Student will learn how to write Java programs to perform simple calculations, they will use Scanner class to obtain input from the console, they will know how to use identifiers to name variables, constants, methods, and classes. The use of constants , Java primitive
…show more content…
English will be the official language throughout the discussion.

Names I.D 1. .……………..………………………………. ………………………………

Activity 1: write a program in java to interchange (swap) values of two variables with a use of 3rd variable.

Input: value num1 and num2
Processing: swapping values of num1 to num2 and num2 to num1 using a third variable num3.
Output: Display value of num1 and num2 after swap operation.
Solution:

A. File -> New project-> Project Name: Lab3Activities , Main Class Name  Swap2Nos  click Finish Button
B. Write Code inside the main method and test it by compiling (F9) and running (Shift + F6) your code.

/* Swap Two Numbers Using Third Vairable*/ public class Swap2Nos { public static void main(String[] args) { // declare variables num1, num2, num3 of the type
…show more content…
int i; float f; double d; i = 1; f = 4.4f; d = 5.5; d = i; i = (int)f; f = (float)d;
System.out.println("i = " + i + " f = " + f + " d = " + d);

The above code does the following:
● Converts the value of i to double and stores it in d. This conversion is done automatically by the compiler, because double data type is normally wider than int, there is absolutely no risk storing int in double.

● In the following two statements, notice that we but a large value into a smaller data type, in this case, a possible loss of data occurs in which we have to be aware of. Because that, compiler (by default) refuses to store float value in int, or double in float. Because of that, we use casting to tell to compiler that we know what we are doing!

Another way to convert between variables is using some defined methods that converts between variables.
One of them is a well known method that converts a String to integer which is Integer.parseInt() method, the following example shows how to use it.

String s =

You May Also Find These Documents Helpful

  • Satisfactory Essays

    Mat/206 week4

    • 302 Words
    • 2 Pages

    Numbers can get really large and hard to manage. For example 53000000*69000000 would end up as a huge number to be written out. Now if you write it as 5.3*10^7*6.9*10^7 makes it much simpler of a problem which the answer would be 3.657*10^14…

    • 302 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    unit 7 assignment 1

    • 431 Words
    • 2 Pages

    7. Convert the While loop in the following code to a Do-While loop: int x;…

    • 431 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    Keyword that tells the compiler that the data type of the identifier is a number.…

    • 703 Words
    • 3 Pages
    Good Essays
  • Good Essays

    This is our first individual assignment for Programming unit this semester. This assignment was given in December 2013, and submission date is on 10th February 2014.…

    • 1859 Words
    • 8 Pages
    Good Essays
  • Satisfactory Essays

    Today’s lecture will be spent entirely in the computer lab. At the end of lab, submit this worksheet.…

    • 581 Words
    • 3 Pages
    Satisfactory Essays
  • Good Essays

    Programming Homework

    • 366 Words
    • 2 Pages

    The formula that will be used in this program is num_1 * num_2 adding 1 each repetition using num_2 = num_2 +1…

    • 366 Words
    • 2 Pages
    Good Essays
  • Satisfactory Essays

    ece362 lab04

    • 559 Words
    • 2 Pages

    1. Create a new directory lab04 and Keil MDK-ARM project for this lab exercise, name it as lab04.…

    • 559 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Programming is an essential skill taught in university IT programs, yet students approach the subject with some difficulty and anxiety. Without prior experience in a programming language these courses may prove too challenging. The Raspberry Pi provides many projects that expose the student to programming language. Python is a powerful language that is both easy to use and flexible enough to create useful programs with the Pi (Monk, October 30, 2012). Using this language one can develop a plethora of software from productivity programs to games. Learning Python with the Raspberry Pi helps students grasps complex concepts such as strings, variables, and data structures. Using programs created by the student can assist in mastering key programming principles and inspiring creativity. One such project is Pygame…

    • 297 Words
    • 1 Page
    Satisfactory Essays
  • Satisfactory Essays

    d. C++ is partially recognized: mangled names generated by GCC are demangled, as well as inheritance described in dwarf2 is honored.…

    • 197 Words
    • 1 Page
    Satisfactory Essays
  • Powerful Essays

    Chapter 3: Understanding Data Types ..........................................................53 Chapter 4: Understanding C# Statements ....................................................75 Chapter 5: Understanding Reference Data Types ..........................................99 Chapter 6: Making Decisions in Code .........................................................135 Chapter 7: Statement Repetition Using Loops ............................................153 Chapter 8: Arrays ......................................................................................175…

    • 109499 Words
    • 438 Pages
    Powerful Essays
  • Good Essays

    Software Development

    • 554 Words
    • 3 Pages

    The program template represents a complete working Java program, with one or more key lines of code replaced with comments. Read the problem description and examine the sample output; then study the template code. Using the problem-solving tips as a guide, replace the /* */ comments with Java code. Compile and execute the program. Compare your output with the sample output provided. Then answer the follow-up questions. The source code for the template is available at the course website (sclass).…

    • 554 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Arrays “provide the programmer with a way of organising a collection of homogeneous data items (that is, items that have the same type and the same length) into a single data structure. An array, then, is a data structure that is made up of a number of variables all of which have the same data type” (Robertson, 2007). For example, there are 40 students in the first grade. A single variable name such as ‘first’ can be used with all 40 first grade students. “The individual data items that make up the array are referred to as the elements of the array. Elements in the array are distinguished from one another by the use of an index or subscript, enclosed in parentheses, following the array name” (Robertson, 2007), such as ‘first (8)’. This subscript refers to the eighth student in the first grade. The smallest element of an array must also be the size of the largest element, so in using the months of the year as an example, the names of the shorter months must be padded with blank spaces. The month of May must be as long as the month of September.…

    • 748 Words
    • 3 Pages
    Good Essays
  • Better Essays

    inFile.nextInt( ) – gets the next value in the file. This must be an integer or you will get an Input Mismatch Exception . Return data type is int.…

    • 928 Words
    • 10 Pages
    Better Essays
  • Better Essays

    The following program, ArrayDemo, creates an array of integers, puts some values in it, and prints each value to standard output.…

    • 1094 Words
    • 5 Pages
    Better Essays
  • Satisfactory Essays

    Cse Cycle Sheet Programs

    • 384 Words
    • 2 Pages

    PROBLEM STATEMENT All the Programs Must be Implemented in C Programming Language Only Write a Program to Display your details in the following format. ******************************* | Name : XXXXXXXX | | Age : XX | | Register Num : XXXXXXXX | | School : XXXXXXXX | ******************************** Write a Program to get Phone number (Integer), Age (Integer), Height (Decimal), Sex (Single character ‐ ‘M’ or ‘F’) from the user and then display the details. Write a Program to Exchange values in two integer variables i) Using a Temporary Variable ii) Without using a Temporary Variable Write a Program to perform basic Arithmetic operations which are Addition, Subtraction, Multiplication, Division and Modulo of Two numbers. Numbers are assumed to be Integers and has to be inputted from the user.…

    • 384 Words
    • 2 Pages
    Satisfactory Essays

Related Topics