Thursday, June 26, 2014


CS5B09: Java Programming

Module 1 Notes

https://drive.google.com/file/d/0BwZENQ95TpvfVlAxTFBvRTZ6Z05nOVZzRGpyRWtza3pTclpF/edit?usp=sharing

Monday, June 16, 2014

Java : Primitive Data Types

Primitive Data Types

The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. This involves stating the variable's type and name, as you've already seen:
int gear = 1;
Doing so tells your program that a field named "gear" exists, holds numerical data, and has an initial value of "1". A variable's data type determines the values it may contain, plus the operations that may be performed on it. In addition to int, the Java programming language supports seven other primitive data types. A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are:
  • byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.
  • short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
  • int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use intdata type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigneddivideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.
  • long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. The unsigned long has a minimum value of 0 and maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods likecompareUnsigneddivideUnsigned etc to support arithmetic operations for unsigned long.
  • float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.
  • double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in theFloating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
  • boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.
  • char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
In addition to the eight primitive data types listed above, the Java programming language also provides special support for character strings via the java.lang.String class. Enclosing your character string within double quotes will automatically create a new String object; for example, String s = "this is a string";String objects areimmutable, which means that once created, their values cannot be changed. The String class is not technically a primitive data type, but considering the special support given to it by the language, you'll probably tend to think of it as such. You'll learn more about the String class in Simple Data Objects

Default Values

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.
The following chart summarizes the default values for the above data types.
Data TypeDefault Value (for fields)
byte0
short0
int0
long0L
float0.0f
double0.0d
char'\u0000'
String (or any object)  null
booleanfalse

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

Literals

You may have noticed that the new keyword isn't used when initializing a variable of a primitive type. Primitive types are special data types built into the language; they are not objects created from a class. A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation. As shown below, it's possible to assign a literal to a variable of a primitive type:
boolean result = true;
char capitalC = 'C';
byte b = 100;
short s = 10000;
int i = 100000;

Integer Literals

An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letterl is hard to distinguish from the digit 1.
Values of the integral types byteshortint, and long can be created from int literals. Values of type long that exceed the range of int can be created from long literals. Integer literals can be expressed by these number systems:
  • Decimal: Base 10, whose digits consists of the numbers 0 through 9; this is the number system you use every day
  • Hexadecimal: Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F
  • Binary: Base 2, whose digits consists of the numbers 0 and 1 (you can create binary literals in Java SE 7 and later)
For general-purpose programming, the decimal system is likely to be the only number system you'll ever use. However, if you need to use another number system, the following example shows the correct syntax. The prefix 0x indicates hexadecimal and 0b indicates binary:
// The number 26, in decimal
int decVal = 26;
//  The number 26, in hexadecimal
int hexVal = 0x1a;
// The number 26, in binary
int binVal = 0b11010;

Floating-Point Literals

A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.
The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).
double d1 = 123.4;
// same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1  = 123.4f;

Character and String Literals

Literals of types char and String may contain any Unicode (UTF-16) characters. If your editor and file system allow it, you can use such characters directly in your code. If not, you can use a "Unicode escape" such as '\u0108' (capital C with circumflex), or "S\u00ED Se\u00F1or" (Sí Señor in Spanish). Always use 'single quotes' for charliterals and "double quotes" for String literals. Unicode escape sequences may be used elsewhere in a program (such as in field names, for example), not just in char orString literals.
The Java programming language also supports a few special escape sequences for char and String literals: \b (backspace), \t (tab), \n (line feed), \f (form feed), \r(carriage return), \" (double quote), \' (single quote), and \\ (backslash).
There's also a special null literal that can be used as a value for any reference type. null may be assigned to any variable, except variables of primitive types. There's little you can do with a null value beyond testing for its presence. Therefore, null is often used in programs as a marker to indicate that some object is unavailable.
Finally, there's also a special kind of literal called a class literal, formed by taking a type name and appending ".class"; for example, String.class. This refers to the object (of type Class) that represents the type itself.

Using Underscore Characters in Numeric Literals

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example. to separate groups of digits in numeric literals, which can improve the readability of your code.
For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.
The following example shows other ways you can use the underscore in numeric literals:
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi =  3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
You can place underscores only between digits; you cannot place underscores in the following places:
  • At the beginning or end of a number
  • Adjacent to a decimal point in a floating point literal
  • Prior to an F or L suffix
  • In positions where a string of digits is expected
The following examples demonstrate valid and invalid underscore placements (which are highlighted) in numeric literals:
// Invalid: cannot put underscores
// adjacent to a decimal point
float pi1 = 3_.1415F;
// Invalid: cannot put underscores 
// adjacent to a decimal point
float pi2 = 3._1415F;
// Invalid: cannot put underscores 
// prior to an L suffix
long socialSecurityNumber1 = 999_99_9999_L;

// OK (decimal literal)
int x1 = 5_2;
// Invalid: cannot put underscores
// At the end of a literal
int x2 = 52_;
// OK (decimal literal)
int x3 = 5_______2;

// Invalid: cannot put underscores
// in the 0x radix prefix
int x4 = 0_x52;
// Invalid: cannot put underscores
// at the beginning of a number
int x5 = 0x_52;
// OK (hexadecimal literal)
int x6 = 0x5_2; 
// Invalid: cannot put underscores
// at the end of a number
int x7 = 0x52_;

Tuesday, February 18, 2014


Shell Programming Practical List

III Semester BSc Computer Science (CCSS-UG 2012 Admission), University of Calicut


       1. Write a script to find the area of a circle

     2. Write a shell script to check whether the given number is even or odd

     3. Write a shell script to make a menu driven calculator using case
       
     4. Write a shell script to find the largest among three numbers

     5. Write a shell script to find the sum of all digits of a given number

      6. Write a shell script to find the reverse of a number

     7. Write a shell script to find prime numbers up to a given number

8. Write a shell script to check whether a given number is Armstrong or not

     9. Write a shell script to check whether a given number is Perfect or not
 
   10. Write a shell script to generate multiplication table of a given integer.


   11. Write a shell script to count no of lines, words and characters of an input file



   12. Write a shell script to find the factorial of a given number


13. Write a shell script to Display Banner, calendar of a given year

14. Code for a program to display current date and time, number of users, terminal name, login date and time


    15.  Code for Shell script to perform operations like display, list, make directory, copy, rename, delete etc.

    16. Write a shell script to accept student number, name and marks in 5 subjects. Find total, average and grade.

        Rules:  Avg>=90 then grade A
        Avg<90 && Avg>=80 then grade B
        Avg<80 && Avg>=60 then grade C
        Avg<60 && Avg>=40 then grade D
        Avg<40 then grade E

Thursday, November 21, 2013

Mobile OS : Concepts




Mobile operating systems


A mobile operating system, also referred to as mobile OS, is the Operating System that operates a smart phone, tablet, PDA, or other digital mobile device. Modern mobile operating systems combine the features of a personal computer operating system with other features, including a touch screen, cellular, Bluetooth, WiFi, GPS mobile navigation, camera, video camera, speech recognition, voice recorder, music player, Near field communication and Infrared Blaster.
Mobile devices with mobile communications capabilities (eg smartphones) contain two mobile operating systems - the main user-facing software platform is supplemented by a second low-level proprietary real-time operating system which operates the radio and other hardware. Research has shown that these low-level systems may contain a range of security vulnerabilities permitting malicious base stations to gain high levels of control over the mobile device.[1]

History

Mobile operating system milestones mirror the development of mobile phones and smartphones:
Common software platforms

The most common mobile operating systems are:

Android

Android is from Google Inc. It is free and open source.Android's releases prior to 2.0 (1.0, 1.5, 1.6) were used exclusively on mobile phones. Most Android phones and some Android tablets, now use a 2.x release. Android 3.0 was a tablet-oriented release and does not officially run on mobile phones. The current Android version is 4.4. Android's releases are nicknamed after sweets or dessert items like Cupcake (1.5), Donut(2.0) Eclair(2.1) Frozen Yogurt ("Froyo") (2.2), Ginger Bread (2.3), Honeycomb (3.0), Ice Cream Sandwich (4.0), Jelly Bean (4.1),(4.2),(4.3) and Kit Kat (4.4). Most major mobile service providers carry an Android device. Since HTC Dream was introduced, there has been an explosion in the number of devices that carry Android OS. From Q2 of 2009 to the second quarter of 2010, Android's worldwide market share rose 850% from 1.8% to 17.2%. On November 15, 2011, Android reached 52.5% of the global smartphone market share.

Blackberry

BlackBerry 10 is from BlackBerry. It is closed source and proprietary. BlackBerry 10 (previously BlackBerry BBX) was the next generation platform for BlackBerry smartphones and tablets. One OS was planned for both Blackberry smartphones and tablets going forward.

iOS

iOS is from Apple Inc. It is closed source and proprietary and built on open source Darwin core OS. The Apple iPhone, iPod Touch, iPad and second-generation Apple TV all use an operating system called iOS, which is derived from Mac OS X. Native third party applications were not officially supported until the release of iOS 2.0 on July 11, 2008. Before this, "jailbreaking" allowed third party applications to be installed, and this method is still available. Currently all iOS devices are developed by Apple and manufactured by Foxconn or another of Apple's partners.

Windows Phone

Windows Phone is from Microsoft. It is closed source and proprietary. On February 15, 2010, Microsoft unveiled its next-generation mobile OS, Windows Phone. The new mobile OS includes a completely new over-hauled UI inspired by Microsoft's "Metro Design Language". It includes full integration of Microsoft services such as Microsoft SkyDrive and Office, Xbox Music, Xbox Video, Xbox Live games and Bing, but also integrates with many other non-Microsoft services such as Facebook and Google accounts. Windows Phone devices are made primarily by Nokia, along with HTC, Samsung, Huawei and other OEMs.


Other Platforms

S40
S40 (Series40) is from Nokia. It is closed source and proprietary. Nokia uses S40 OS in their feature phones. Over the years, more than 150 phone models have run S40 OS.[18] Since the introduction of S40 OS it has evolved from monochrome low resolution UI to a full touch 256k color UI.

Symbian OS

Symbian OS is from Nokia and Accenture.[5] It uses an open public license. Symbian has the largest smartphone share in most markets worldwide, but lags behind other companies in the relatively small but highly visible North American market.[20] This matches the success of Nokia in all markets except Japan. In Japan Symbian is strong due to a relationship with NTT DoCoMo, with only one of the 44 Symbian handsets released in Japan from Nokia.[21] It has been used by many major handset manufacturers, including BenQ, Fujitsu, LG, Mitsubishi, Motorola, Nokia, Samsung, Sharp and Sony Ericsson. Current Symbian-based devices are being made by Fujitsu, Nokia, Samsung, Sharp and Sony Ericsson. Prior to 2009 Symbian supported multiple user interfaces, i.e. UIQ from UIQ Technologies, S60 from Nokia and MOAP from NTT DOCOMO. As part of the formation of the Symbian OS in 2009 these three UIs were merged into a single OS which is now fully open source. Recently, though shipments of Symbian devices have increased, the operating system's worldwide market share has declined from over 50% to just over 40% from 2009 to 2010. Nokia handed the development of Symbian to Accenture, which will support the OS until 2016.[22]

webOS

webOS is from LG, although some parts are open source. webOS is a proprietary mobile operating system running on the Linux kernel, initially developed by Palm, which launched with the Palm Pre. After being acquired by HP, two phones (the Veer and the Pre 3) and a tablet (the TouchPad) running webOS were introduced in 2011. On August 18, 2011, HP announced that webOS hardware was to be discontinued[26] but would continue to support and update webOS software and develop the webOS ecosystem.[27] HP released webOS as open source under the name Open webOS, and plans to update it with additional features.[28] On February 25, 2013 HP announced the sale of WebOS to LG Electronics, who planned to use the operating system for its "smart" or Internet-connected TVs. However HP retained patents underlying WebOS as well as cloud-based services such as the App Catalog.

Mobile Operating System Structure 


Applications
OS Libraries
Device Operating System Base, Kernel
Low level Hardware, Device Drivers




Ref: From Wikipedia, the free encyclopedia

Thursday, October 17, 2013

OS: Scheduling Algorithms

Scheduling Algorithms

First-Come, First-Served (FCFS) Scheduling
       
         Process          Burst Time
          P1                                24
          P2                                  3
          P3                                  3

Suppose that the processes arrive in the order: P1 P2 P3

The Gantt Chart for the schedule is:
        
  
P1
P2
P3
0                                                  24                                    27                                                       30

  Waiting time for P1 = 0; P2 = 24; P3 = 27
  Average waiting time: (0 + 24 + 27)/3 = 17

  Suppose that the processes arrive in the order
  P2 P3 P1 .

The Gantt chart for the schedule is:

P2
P3
P1
0                                                      3                                           6                                               30

  Waiting time for P1 = 6; P2 = 0; P3 = 3
  Average waiting time: (6 + 0 + 3)/3 = 3
  
Much better than previous case.

 Convoy effect short process behind long process
  
Shortest-Job-First (SJF) Scheduling

Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time.

Two schemes:

1. Non pre- emptive – once CPU given to the process it cannot be preempted until completes its CPU burst.

2. Preemptive – Preemption takes place when a new process arrives with CPU burst length less than remaining time of current executing process. This scheme is also known as the Shortest-Remaining-Time-Next (SRTN) Scheduling.

SJF is optimal – gives minimum average waiting time for a given set of processes.

                Process          Arrival Time     Burst Time
                    P1                    0.0                         7
                    P2                    2.0                         4
                    P3                    4.0                         1
                    P4                    5.0                         4

SJF (non-preemptive)

P1
P3
P2
P4
0                                  7                               8                         12                                     16

Average waiting time = [0 +(8-2)+(7-4) +(12-5)] /4 =4

Example of Preemptive SJF

Proces                        Arrival  Time               Burst Time
P1                                    0.0                               7
P2                                    2.0                               4
P3                                    4.0                               1
P4                                     5.0                              4

SJF (preemptive)

P1
P2
P3
P2
P4
P1
0                   2                       4                     5                         7                      11               16

        Average waiting time = (9 + 1 + 0 +2)/4 =3
       

Determining length of cpu burst is possiblee by using the length of previous CPU bursts, using exponential averaging though it's a very complex calculation to carry out.



Priority Scheduling

A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest priority (smallest integer ≡ highest priority).
       1. Preemptive
       2. nonpreemptive
SJF is a priority scheduling where priority is the predicted next CPU burst time.
Problem ≡ Starvation – low priority processes may never execute.
Solution ≡ Aging – as time progresses increase the priority of the process.

Round Robin (RR)

Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time  has elapsed, the process is preempted and added to the end of the ready queue.
If there are processes in the ready queue and the time quantum is q, then each process gets 1/of the
CPU time in chunks of at most time units at once. No process waits more than (n-1)time units.
Performance
        1. large FIFO
        2. small must be large with respect to context switch, otherwise overhead is too high.
Example of RR with Time Quantum = 4

                        Process    Burst Time
                            P1                    24
                            P2                     3
                            P3                     3

The Gantt chart is:
P1
P2
P3
P1
P1
P1
P1
P1
0          4               7              10            14            18             22          26            30

Average waiting time =    ((30-24)+4+7)/3  = 17/3 =5.66
     
Multilevel Queue

Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
Each queue has its own scheduling algorithm,
foreground – RR
background – FCFS
Scheduling must be done between the queues.
  1. Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation.
  2. Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes; i.e., 80% to foreground in RR
1. 20% to background in FCFS

Multilevel Queue Scheduling

      

Multilevel Feedback Queue

A process can move between the various queues; aging can be implemented this way.
Multilevel-feedback-queue scheduler defined by the following parameters:
   1. number of queues
   2. scheduling algorithms for each queue
   3. method used to determine when to upgrade a process
   4. method used to determine when to degrade a process
   5. method used to determine which queue a process will enter  when that process needs service



          


Three queues:
    1. Q0 – time quantum 8 milliseconds
    2. Q1 – time quantum 16 milliseconds
    3. Q2 – FCFS
Scheduling
    1. A new job enters queue Q0 which is served FCFS . When it gains CPU, job receives 8 milliseconds.
        If it does not finish in 8 milliseconds, job is moved to queue Q1.
    2. At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete,
         it is preempted and moved to queue Q2.