0% found this document useful (0 votes)
45 views17 pages

PF 2

The document discusses programming fundamentals concepts including: 1. Explaining the purpose of main method, variable declarations, and initializations. 2. Listing the value ranges of common Java primitive data types. 3. Explaining the purpose of escape sequences used in Java. 4. Providing examples of arithmetic operations and operators in Java code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views17 pages

PF 2

The document discusses programming fundamentals concepts including: 1. Explaining the purpose of main method, variable declarations, and initializations. 2. Listing the value ranges of common Java primitive data types. 3. Explaining the purpose of escape sequences used in Java. 4. Providing examples of arithmetic operations and operators in Java code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Programming fundamentals

Assignment No II

1. Explain the purpose of following statements:

ASSIGNMENT # 02
public static void main (String args)

\\ declaring access specifier for a class

int count;

\\ integer declaration

String title;

\\ string declaration

boolean isAsleep;
NAME: TALAL IQBAL
\\ Boolean declaration
REGISTERATION
int myAge,NO: FA21-BCT-017

PROGRAM:mySize,
CYBER SECURITY
numShoes = 28;

\\ integer declaration/initialization
SUBJECT: PROGRAMMING
String myName = “Laura”;
FUNDAMENTALS
SUBMITTED TO:initialization
\\ string Ms. SADIA KHAN
boolean isTired = true;
DATE OF SUBMISSION: 3/8/2021
\\ Boolean initialization

int a = 4, b = 5, c = 6;

\\ integer declaration/initialization

}
2. Write the range of following variables:

Byte, short, int, long, float, char, Boolean

Data Description
Type

byte Stores whole numbers from -128 to 127

short Stores whole numbers from -32,768 to 32,767

int Stores whole numbers from -2,147,483,648 to 2,147,483,647

long Stores whole numbers from -9,223,372,036,854,775,808 to


9,223,372,036,854,775,807

float Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits

boolean Stores true or false values

char Stores a single character/letter or ASCII values

3. What is the purpose of following escape sequences:

\n: sends prompt on new line

\t: print 8 spaces


\b: sends prompt back one space

\r: carriage return

\f: prints formfeed

\\: prints \ (slash)

\’: prints ‘ (comma)

\”: print “ (double)

\ddd:

\xdd:

\udddd:

4. Write the output of following code:

a. class ArithmeticTest

public static void main (String[] args) {

short x = 6;

int y = 4;

float a = 12.5f;

float b = 7f;

System.out.println(“x is “ + x + “, y is “ + y);

System.out.println(“x + y = “ + (x + y));

System.out.println(“x - y = “ + (x - y));

System.out.println(“x / y = “ + (x / y));

System.out.println(“x % y = “ + (x % y));

System.out.println(“a is “ + a + “, b is “ + b;

System.out.println(“a / b = “ + (a / b)); 16: }

OUTPUT:
x is 6 y is 4

x+y=10

x-y=2

x/y=1

x%y=2

a is 12.5 b is 7.0

a/b=1.7857143

b.

OUTPUT: 
x  and y are 0 and 0 
x++ results in 1 
x++ results in 2 
Resetting x back to 0. 
_ _ _ _ _ _ 
y = x++ (postfix) results in: 
x is 1 
y is 0 
_ _ _ _ _ _ 
y = ++x (prefix) results in:   
x is 2 
y is 2 

5. Explain the purpose of following operators by using them in program/s;

class Qs{ 
Public static void main(String[] args){ 
Int x ,y; 
x=5+5; 
  x = 5-5; 
x =5*5; 
 x = 5/5; 
 x = 5%5; 
  if (1<0){ 
System.out.print(“HELLO”); 

  if (1<0){ 
System.out.print(“HELLO”); 

 if (1<=0){ 
System.out.print(“HELLO”); 

 if (1>=0){ 
System.out.print(“HELLO”); 

  if (1==0){ 
System.out.print(“HELLO”); 

  if (1!=0){ 
System.out.print(“HELLO”); 

 if (1<0 && 1>3){ 
System.out.print(“HELLO”); 

  if (1<0 || 1>3){ 
System.out.print(“HELLO”); 

x = x & 3; 
x= x | 3; 
y = !(1>5); 
System.out.println(10<<2);//10*2^2 
System.out.println(10>>2);//10/2^2 same as 10>>>2 
x++; 
y--; 
x+=1; 
y-=1; 
x%=6; 
y*=7; 
y>>=3; 
x<<=2; 
x>>>3; 
 } 

6. Write a program to calculate the salary as per the following table:


import java.util.Scanner;

class gg {

public static void main(String[] args)

int num,years,gender;

Scanner input = new Scanner(System.in);

System.out.print("Press 1 if you are Female, 2 if you are Male: ");

gender = input.nextInt();

System.out.print("Press 1 if you're qualification is Graduate, 2 if you're


qualification is Post-graduate: ");

num = input.nextInt();
System.out.print("Enter your years of service: ");

years = input.nextInt();

if(gender==2){

if (num==1){

if(years>=10){

System.out.print("Your salary is 10000.");

else {

System.out.print("Your salary is 7000.");

else if (years>=10) {

System.out.print("Your salary is 15000.");

else {
System.out.print("Your salary is 10000.");

else if (num==1) {

if(years>=10){

System.out.print("Your salary is 9000.");

else{

System.out.print("Your salary is 6000.");

else if (years>=10) {

System.out.print("Your salary is 12000.");

else{

System.out.print("Your salary is 10000.");


}

7. Write the output of following program


Public static void main( )
{
int m1, m2, m3, m4, m5, per ;
System.out.println ( "Enter marks in five subjects " ) ;
Scanner input= new Scanner(System.in);
int m1= input.nextInt();

int m2= input.nextInt();


int m3= input.nextInt();
int m4= input.nextInt();
int m5= input.nextInt();
int per= input.nextInt();

per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
System.out.printf ( "First division ") ;
else
{
if ( per >= 50 )
System.out.printf ( "Second division" ) ;
else
{
if ( per >= 40 )
System.out.printf ( "Third division" ) ;
else
System.out.printf ( "Fail" ) ;
}
}
}

OUTPUT: 
Enter marks in five subjects 
50 
50 
50 
50 
50 
50 
Second division 

8. A company insures its drivers in the following cases:


− If the driver is married.
− If the driver is unmarried, male & above 30 years of age.
− If the driver is unmarried, female & above 25 years of age.
In all other cases the driver is not insured. If the marital status, gender and age of the
driver are the inputs, write a java program to determine whether the driver is to be
insured or not.

import java.util.Scanner; 
class main{ 
public static void main(String[] args )   
{    
int status,age,gender;  
Scanner input = new Scanner(System.in);  
System.out.print("Press 1 if you are Female, 2 if you are Male: ");  
gender = input.nextInt();  
System.out.print("Press 1 if you're married, 2 if you're single: ");  
status = input.nextInt(); 
System.out.print("Enter your age: "); 
age = input.nextInt(); 
if(status==1){ 
System.out.print("You will be given insurance."); 

else if(status==2){ 
if(gender==2){ 
if(age>30){ 
System.out.print("You will be given insurance."); 

else{ 
System.out.print("No insurance."); 


else if(gender==1){ 
if(age>25){ 
System.out.print("You will be given insurance."); 

else{ 
System.out.print("No insurance."); 



 

9. Write the output of following:
char a ;
int y ;

Scanner input= new Scanner(System.in);

Char a= input.nextChar();

y = ( a >= 65 && a <= 90 ? 1 : 0 ) ;

No output because of missing output statement 

int x, y ;
Scanner input= new Scanner(System.in);

Int a= input.nextInt();

y=(x>5?3:4);

No output because of missing output statement 

10.

import java.util.Scanner;

class gg{

public static void main(String[] args )

{ int ch;
Scanner input = new Scanner(System.in);
System.out.print("Enter ASCII value of a Character = ");
ch = input.nextInt();
if (ch >= 65 && ch <= 92)
System.out.print("Capital letter");
else if (ch >= 97 && ch <= 122)
System.out.print("Small letter");
else if (ch >= 48 && ch <= 57)
System.out.print("Digit");
else
System.out.print("Special character");
}
}

11. A library charges a fine for every book returned late. For first 5 days the fine is 50 paise, for
6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after
30 days your membership will be cancelled. Write a program to accept the number of days
the member is late to return the book and display the fine or the appropriate message.

import java.util.Scanner; 
class main{ 
public static void main(String[] args )   
{    
int days; 
Scanner input = new Scanner(System.in); 
System.out.print("Enter the number of days you are late: "); 
days = input.nextInt(); 
if(days<=5){ 
System.out.print("Your fine is 50 paisa."); 

else if(days<=10){ 
System.out.print("Your fine is 1 rupee."); 

else if(days>10 && days<=30){ 
System.out.print("Your fine is 5 rupee."); 

else if(days>30){ 
System.out.print("Your membership is cancelled."); 


12. A university has the following rules for a student to qualify for a degree with A as the main
subject and B as the subsidiary subject:
(a) He should get 55 percent or more in A and 45 percent or more in B.
(b) If he gets than 55 percent in A he should get 55 percent or more in B. However, he
should get at least 45 percent in A.
(c) If he gets less than 45 percent in B and 65 percent or more in A he is allowed to
reappear in an examination in B to qualify.
(d) In all other cases he is declared to have failed. Write a program to receive marks in A
and B and Output whether the student has passed, failed or is allowed to reappear in B.

import java.util.Scanner; 
class main{ 
public static void main(String[] args )   
{    
int A,B; 
Scanner input = new Scanner(System.in); 
System.out.print("Enter your percentage in A :"); 
A = input.nextInt(); 
System.out.print("Enter your percentage in B :"); 
B = input.nextInt(); 
if(A>=55 && B>=45){ 
System.out.print("You have passed."); 

else if(A<55 && A>=45 && B>=55){ 
System.out.print("Your have passed."); 

else if(B<45 && A>=65){ 
System.out.print("You are allowed to reappear in examination B."); 

else{ 
System.out.print("You have failed."); 

 

13. Rewrite the following programs using conditional operators.


1. main( ) {

int x, min, max ;


Scanner input= new Scanner(System.in);

Int x= input.nextInt();

Int max= input.nextInt();

if ( x > max )
max = x ;
else
min = x ;
}

main( ) {
int x, min, max;
Scanner input= new Scanner(System.in);
x= input.nextInt();
max= input.nextInt();
max = (x > max)? x : 0 ;
min = (x < max)? x : 0 ;}

2. {
float sal ;
System.out.printf ("Enter the salary" ) ;
Scanner input= new Scanner(System.in);

float sal= input.nextFloat();

if ( sal < 40000 && sal > 25000 )


System.out.printf ( "Manager" ) ;
else if ( sal < 25000 && sal > 15000 )
System.out.printf ( "Accountant" ) ;
else printf ( "Clerk" ) ;
}

float sal ;
System.out.printf ("Enter the salary" ) ;
Scanner input= new Scanner(System.in);
sal= input.nextFloat();
String result = ( sal < 40000 && sal > 25000 )? "Manager" : ( sal < 25000 && sal >
15000 ) ? "Accountant" : "Clerk";
System.out.print(result);

14. Write a program using conditional operators to determine whether a year entered through
the keyboard is a leap year or not.

import java.util.Scanner;

class gg{

public static void main(String[] args )

int year;

System.out.print("Enter a year: ");

Scanner input = new Scanner(System.in);

year = input.nextInt();

String result = (year%100==0)? (year%400==0)? "This is a leap year." :"This is not a leap
year." : (year%4==0)? "This is a leap year." :"This is not a leap year.";

System.out.print(result);
}

}
15. Write a program to find the greatest of the three numbers entered through the keyboard
using conditional operators.

import java.util.Scanner;
class gg {

public static void main(String[] args)


{

int no1,no2,no3;
Scanner input = new Scanner(System.in);
System.out.println("Enter first number.");
no1 = input.nextInt();
System.out.println("Enter second number.");
no2 = input.nextInt();
System.out.print("Enter third number.");
no3 = input.nextInt();
String greatest = (no1>no2 && no1>no3)? "Greatest number is 1st number" :
(no2>no1 && no2>no3)? "Greatest number is 2nd number " : (no3>no1 && no3>no2)? "Greatest
number is 3rd number ": " ";
System.out.print(greatest);

}
}

You might also like