File

advertisement
Introduction to Software Development
Tutorial 4 –
Introductory Programming Skills
Pair-work: Answer the following questions
1. What is the difference between procedure oriented and object oriented approach to
programming.
Answer:
Procedure Oriented Programming
1. Follows Top down approach.
2. Doesn’t implement data hiding as
all variables are globally available.
3. More focus is on programing.
4. It doesn’t have the features like
abstraction,
encapsulation,
inheritance, etc.
5. Examples: C, Pascal.
Object Oriented Programming
1. Follows Bottom Up approach.
2. Implement data hiding
3. More focus is on data not to the
programing.
4. It have such features.
5. Examples: C++, JAVA, C#.
2. Java is platform independent language. What does this means?
Answer:
Java is a platform independent language as it produces a special format called the magic code or
byte code that can run everywhere.
3. What are the different ways of writing comments in Java?
Answer:
Different ways of writing comments in Java:
1. Single line comment
Syntax: // comments
2. Multi line comments
Syntax: /*
comments
*/
4. What is a difference between a JRE, JVM and JDK?
Answers:JRE:JRE stands for Java Runtime Environment. JRE physically exists and it is the implementation
of JVM. It contains set of libraries and other files which JVM uses at runtime.
JVM:JVM stands for Java Virtual Machine. It provides the runtime environment in which the java
magic code can be executed. It performs main tasks such as it load, verifies and executes code
and it provides run time environment.
JDK:Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 1
Introduction to Software Development
JDK stands for Java Development Kit. It also physically exists. It contains JRE as well as
development tools.
5. What are primitive and non primitive data types in Java?
Answer:
Primitive Data Types are
a. Byte
b. Short
c. Int
d. Long
e. Float
f. Double
g. Char
h. Boolean
Non-Primitive Data Types are
a. Classes
b. Arrays
c. Interface
6. What are keywords/reserve words in Java, how they differ from identifiers.
Answer:
Keywords are the reserve words with combined with the syntax of the operators and separators,
form the foundation of the java language. These keywords cannot be used as names for a
variable, class or method.
Whereas, Identifiers are used for class names, method names and variable names. It is combined
with uppercases and lowercases letters, numbers or the underscore or the dollar sign character.
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 2
Introduction to Software Development
Tutorial 5 –
Understanding Programming Language Constructs
Individual : Write simple Java applications
1. Write a Java application that prints your full name, intake code, your college name and
college address on separate lines of the screen.
Program:
class tut5_1
{
public static void main(String[] args)
{
System.out.println("Name : Krishnakant Kumar");
System.out.println("Intake : PT1381110");
System.out.println("College: APIIT SD INDIA");
System.out.println("
near Toll Plaza, Panipat.");
}
}
Output:
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 3
Introduction to Software Development
2. Write a Java application that finds
a. area and perimeter of rectangle.
b. area and circumference of a circle.
Display explanatory text with the value – for example, The area of rectangle is 375.
Program:
import java.util.Scanner;
class tut5_2
{
Scanner s = new Scanner(System.in);
public static void main(String[] args)
{
tut5_2 t = new tut5_2();
Scanner s = new Scanner(System.in);
int c;
System.out.println("1.Rectangle");
System.out.println("2.Circle");
System.out.print("Enter Your Choice: ");
c = s.nextInt();
switch(c)
{
case 1: t.rec();
break;
case 2: t.cir();
break;
default: System.out.println("Wrong KeyWord Entered......\n\n\n");
}
}
void rec()
{
int l,b;
System.out.println("\n\n.....RECTANGLE.....");
System.out.print("\n\nEnter Length : ");
l = s.nextInt();
System.out.print("Enter Breadth : ");
b = s.nextInt();
System.out.println("Area of Rectangle : "+(l*b));
System.out.println("Perimeter of Rectangle : "+ (2*(l+b)));
}
void cir()
{
int r;
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 4
Introduction to Software Development
System.out.println("\n\n.....CIRCLE.....\n\n");
System.out.print("Enter Radious of Circle : ");
r = s.nextInt();
System.out.println("Area of Circle
: "+(3.14*r*r));
System.out.println("Perimeter of Circle : "+ (2*3.14*r));
}
}
Output:
3. A computer manufacturing company announces a special offer to their customers on
purchasing laptops and printers accordingly
on Laptop : Discount 15%
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 5
Introduction to Software Development
on Printer : Discount 10%
Write a program in Java to calculate the discount, if a customer purchases a laptop and
printer.
Program:
import java.util.Scanner;
class tut5_3
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
long lap, pr;
System.out.print("Enter Laptop price : ");
lap= s.nextLong();
System.out.println("Discount
: 15%");
System.out.println("Price after Discount : "+(lap-lap*0.15));
System.out.print("\n\nEnter Printer price : ");
pr= s.nextLong();
System.out.println("Discount
: 10%");
System.out.println("Price after Discount : "+(pr-pr*0.1)+"\n\n");
}
}
Output:
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 6
Introduction to Software Development
4.
Write a class with the name employee and basic pay as its data member, find gross pay of
an employee for the following allowance and deductions
Dearness allowance = 25% of Basic pay
House rent allowance = 15% of Basic pay
Provident fund=8.33% of Basic pay
Net pay=basic pay + Dearness Allowance + House Rent Allowance
Gross Pay= Net pay – Provident Fund.
Program:
import java.util.Scanner;
class tut5_4
{
public static void main(String[] args)
{
Scanner s= new Scanner(System.in);
double da,hra,pf,np,gp;
System.out.print("Enter Name : ");
String name= s.next();
System.out.print("Enter Salary : ");
double sal=s.nextDouble();
hra=0.15*sal;
pf=0.0833*sal;
da=0.25*sal;
np=da+hra+sal;
gp=np-pf;
System.out.println("PF
: "+pf);
System.out.println("DA
: "+da);
System.out.println("HRA
: "+hra);
System.out.println("Netpay
: "+np);
System.out.println("Grosspay : "+gp);
}
}
Output:
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 7
Introduction to Software Development
5. Write a program to accept the sides of a triangle and display whether it is an equilateral or
isosceles or a scalene triangle.
Program:
import java.util.Scanner;
class tut5_5
{
public static void main(String[] args)
{
Scanner s= new Scanner(System.in);
int a,b,c;
System.out.print("Enter 1st side : ");
a=s.nextInt();
System.out.print("Enter 2nd side : ");
b=s.nextInt();
System.out.print("Enter 3rd side : ");
c=s.nextInt();
if(a==b&&a==c) System.out.println("Equilateral Triangle\n");
else if(a==b||a==c||b==c) System.out.println("Isoceles Triangle\n");
else System.out.println("Scalene Triangle\n");
}
}
Output:
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 8
Introduction to Software Development
6. Write a program to accept three numbers and display the greatest number
Programs:
import java.util.Scanner;
class tut5_6
{
public static void main(String[] args)
{
Scanner s= new Scanner(System.in);
int a,b,c;
System.out.print("Enter 1st no. : ");
a=s.nextInt();
System.out.print("Enter 2nd no. : ");
b=s.nextInt();
System.out.print("Enter 3rd no. : ");
c=s.nextInt();
if(a>b&&a>c) System.out.println(a+" is the Greatest one.");
else if(b>c) System.out.println(b+" is the Greatest one.");
else System.out.println(c+" is the Greatest one.");
}
}
Output:
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 9
Introduction to Software Development
7. In an examination, the grades are given according to the marks obtained. Write a program to
display the grades accordingly.
Marks
80% and above
60% or more but less than 80%
45% or more but less than 60%
40% or more but less than 45%
Less than 40%
Grades
Distinction
First Division
Second Division
Pass
Promotion not Granted
Program:
import java.util.Scanner;
class tut5_7
{
public static void main(String[] args)
{
Scanner s= new Scanner(System.in);
int m;
String gr;
System.out.print("Enter marks (out of 100) : ");
m = s.nextInt();
if(m>=80) gr="Distinction";
else if(m>=60) gr="First Division";
else if(m>=45) gr="Second Division";
else if(m>=40) gr="Pass";
else gr="First Division";
System.out.println("Grade : "+gr);
}
}
Output:
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 10
Introduction to Software Development
Tutorial 6 –
Controlling Program Flow – Selection Control
Individual : Write Java applications using selection control
1. Midland bank offers following rate of interest on fixed deposite:
Time
Rate
Upto 6 months
7%
More than 6 months and upto 12 months
8%
More than 12 months and upto 36 months
9%
More than 36 months
10%
The amount after m months is calculated by using the formula:
A=P*(1+R/100) power n
Using switch case, write a program to compute amount in above cases.
Program:
import java.util.*;
class tut6_1
{
public static void main(String[] args)
{
int r,ch,c=0;
double p,a=0,t,pp;
Scanner s=new Scanner(System.in);
System.out.print("Enter Principle amount : ");
p = s.nextInt();
System.out.println("\n\n Menu");
System.out.println("1. Upto 6 months, rate=7%");
System.out.println("2. More than 6 months and upto 12 months, rate=8%");
System.out.println("3. More than 12 months and upto 36 months, rate=9%");
System.out.println("4. More than 36 months, rate=10%");
System.out.print("Enter choice : ");
ch = s.nextInt();
System.out.print("Enter time (in month): ");
t = s.nextFloat();
if(t<=6&&ch==1) c=1;
else if(t>6&&t<=12&&ch==2) c=2;
else if(t>12&&t<=36&&ch==3) c=3;
else if(t>36&&ch==4)c=4;
else System.out.println("Your choice and time does not match.");
switch(c)
{
case 1:{
pp=Math.pow(1.07f,(t/12f));
a=p*pp;
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 11
Introduction to Software Development
System.out.println("Amount : " + (float)a );
}
break;
case 2:{
pp=Math.pow(1.08f,(t/12f));
a=p*pp;
System.out.println("Amount : " + (float)a );
}
break;
case 3:{
pp=Math.pow(1.09f,(t/12f));
a=p*pp;
System.out.println("Amount : " + (float)a );
}
break;
case 4:{
pp=Math.pow(1.10f,(t/12f));
a=p*pp;
System.out.println("Amount : " + (float)a );
}
}
}
}
Output:
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 12
Introduction to Software Development
2. Write a program to display the color of the spectrum (VIBGYOR) according to the
user’s choice.
Program:
import java.util.Scanner;
class tut6_2
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter color code ( V I B G Y O R ): ");
String ch= s.next();
char c=ch.charAt(0);
switch(c)
{
case 'v':
case 'V': System.out.println("Voilet");
break;
case 'i':
case 'I': System.out.println("Indigo");
break;
case 'b':
case 'B': System.out.println("Blue");
break;
case 'g':
case 'G': System.out.println("Green");
break;
case 'y':
case 'Y': System.out.println("Yellow");
break;
case 'o':
case 'O': System.out.println("Orange");
break;
case 'r':
case 'R': System.out.println("Red");
break;
default: System.out.println("You Enter a Wrong Color Code....");
}
}
}
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 13
Introduction to Software Development
Output:
3. Haryana Electricity Board(HSEB) charges for electricity from their consumers
according to the units consumed (per month) as per the given tariff:
Units Consumed
Charges
Upto 100 units
Rs 1.35/unit
>100 units and upto 200 units
Rs 1.50/unit
>200 units
Rs 1.80/unit
Write a program to calculate the monthly Electricity Bill.
Program:
import java.util.Scanner;
class tut6_3
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter units consumed : ");
int u = s.nextInt();
float chrg;
if(u<=100) chrg=1.35f;
else if (u<=200) chrg=1.50f;
else chrg=1.80f;
System.out.println("Monthly Electricity Bill : "+ (chrg*u));
}
}
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 14
Introduction to Software Development
Output:
4. Write a program that calculates and prints the bill for a cellular telephone company. The
company offers two types of service: regular and premium. Rates vary based on the type
of service and are computed as follows:
a) Regular service: $10.00 plus first 50 minutes are free. Charges for over 50 minutes
are $0.20 per minutes.
b) Premium service: $25.00 plus:
i.
For calls made from 6.00am to 6.00pm, the first 75 minutes are free; charges
for over 75 minutes are $0.10 per minute.
ii. For calls made from 6.00pm to 6.00am, the first 100 minutes are free;
charges for over 100 minutes are $0.05 per minute.
Your program should prompt the user to enter an account number, a service code r or R
for regular service and p or P for premium service. Treat any other character as an error
and the number of minutes the service was used during the day and the night (for
premium service only). Your program should output the account number, type of
services, number of minutes the telephone service was used and the amount due from
the user.
Program:
import java.util.Scanner;
class tut6_4
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 15
Introduction to Software Development
System.out.print("Enter account no. : ");
long ano = s.nextLong();
System.out.println("1. Regular Service");
System.out.println("2. Premium Service");
System.out.print("Enter choice : ");
String ch=s.next();
char c=ch.charAt(0);
switch(c)
{
case 'R':
case 'r':{ double chrg=10;
System.out.print("Enter no. of minutes used : ");
int min=s.nextInt();
if(min>50)
chrg+=(min-50)*0.20;
System.out.println("\n\n Your Account BiLL");
System.out.println("Account no. : "+ano);
System.out.println("Account Type : Regular");
System.out.println("No. of minutes Used : "+min);
System.out.println("total charges : "+chrg);
}
break;
case 'p':
case 'P':{ double chrg=25;
System.out.print("\n\nEnter Day minutes used : ");
int dmin=s.nextInt();
System.out.print("\nEnter Nights minutes used :");
int nmin=s.nextInt();
if(dmin>75)
chrg+=(dmin-75)*0.1;
if(nmin>100)
chrg+=(nmin-100)*0.05;
System.out.println("\n\n Your Account BiLL");
System.out.println("Account no. : "+ano);
System.out.println("Account Type : Premium");
System.out.println("No. of day minutes Used : "+dmin);
System.out.println("No. of night minutes Used : "+nmin);
System.out.println("total charges : "+chrg);
}
break;
default: System.out.println("Error... Wrong Keyword Entered.");
}
}
}
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 16
Introduction to Software Development
Output:
5. Write a program that allows to accept any value form 1 to 7 and display the weekdays
corresponding to the number entered from the keyboard.
Program:
import java.util.Scanner;
class tut6_5
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter Week no. : ");
int ch=s.nextInt();
switch(ch)
{
case 1:System.out.println("SUNDAY");
break;
case 2:System.out.println("MONDAY");
break;
case 3:System.out.println("TUESDAY");
break;
case 4:System.out.println("WEDNESDAY");
break;
case 5:System.out.println("THURSDAY");
break;
case 6:System.out.println("FRIDAY");
break;
case 7:System.out.println("SATURDAY");
break;
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 17
Introduction to Software Development
default:System.out.println("THis is not a week no......");
}
}
}
Output:
6. A cloth showroom has announced the following festival discount on the purchase of
items, based on the total cost of the items purchased:
Total Cost
Upto Rs 2000
Rs 2001 to Rs 5000
Rs 5001 to Rs 10000
Above Rs 10000
Discount(in percentage)
5%
25%
35%
50%
Write a program to input the total cost and to compute and display the amount to be paid
by the customer after availing the discount.
Program:
import java.util.Scanner;
class tut6_6
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter Cost of cloth : ");
float c,d;
c=s.nextFloat();
if(c<=2000) d=c*0.05f;
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 18
Introduction to Software Development
else if(c<=5000) d=0.25f*c;
else if(c<=10000) d=0.35f*c;
else d=0.5f*c;
System.out.println("Discount :"+d );
System.out.println("Amount paid : "+(c-d));
}
}
Output:
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 19
Introduction to Software Development
Tutorial 7 – Controlling Program Flow – Iterative Control
Individual: Answer the following questions
1. Write a program to display the following patterns
a) 1
b) 1 2 3 4 5 6 7 8 9
22
1234567
333
12345
4444
123
55555
1
Program:
a)
class tut7_1
{
public static void main(String[] args)
{ for(int i=1;i<6;i++)
{
for(int j=1;j<=i;j++)
System.out.print(i);
System.out.println("");
}
}
}
Output
b) class tut7_1_2
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 20
Introduction to Software Development
{
public static void main(String[] args)
{
for(int i=9;i>0;i-=2)
{
for(int j=1;j<=i;j++)
System.out.print(j);
System.out.println("");
}
}
}
Output
2. Write a program to find the sum of the given series:
a) 1+1/2+1/3+ ………………….+1/20
b) 1/2+2/3+3/4+………………….+19/20
Program:
a) import java.util.Scanner;
class tut7_2_1
{
public static void main(String[] args)
{
double num=1,sum=0;
Scanner s=new Scanner(System.in);
System.out.print("Sum of The SERIES \n\n1");
for(;num<21;num++)
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 21
Introduction to Software Development
{
sum+=(1/num);
if((num+1)!=21)
System.out.print("+ 1/"+(int)(num+1));
}
System.out.println("\n\nSum : "+sum+"\n\n");
}
}
Output
b) import java.util.Scanner;
class tut7_2_2
{
public static void main(String[] args)
{
double num=1,num1=2,sum=0;
Scanner s=new Scanner(System.in);
System.out.print("Sum of The SERIES \n\n");
for(;num1<21;num++,num1++)
{
sum+=(num/num1);
System.out.print((int)num+"/"+(int)num1+" + ");
}
System.out.println("\n\nSum : "+sum+"\n\n");
}
}
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 22
Introduction to Software Development
Output
3. Write a program that prompts the user to input a set of integers and then finds and prints
the sum of the odd and even integers.
Program:
import java.util.Scanner;
class tut7_3
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int j,e=0,o=0;
do
{
System.out.print("Enter a num. (0 to show result): ");
j = s.nextInt();
if(j%2==0)
e+=j;
else o+=j;
}
while (j!=0);
System.out.println("Sum of odd num. : "+o);
System.out.println("Sum of even num. : "+e);
}
}
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 23
Introduction to Software Development
Output
4. Write a program to find the factorial of 10
Factorial of 10! =10*9*8……………….*1
Program:
import java.util.Scanner;
class tut7_4
{
public static void main(String[] args)
{
System.out.println("Factorial of 10 : \n");
long fact=1;
for(int i=10;i>0;i--)
{
fact*=i;
System.out.print(i+"*");
}
System.out.print("\b : "+fact+"\n\n2");
}
}
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 24
Introduction to Software Development
Output
5. Write a program to accept the monthly sales of 40 salesman of an advertising company
and calculate the commission of each salesman according to the following table :
Sales (Rs.)
Up to Rs 5000
> Rs. 5000 and upto Rs. 10000
>Rs. 10000 and upto Rs. 15000
>Rs. 15000
Commission
Nil
12%
15%
18%
Apart from this, each salesman gets travelling allowance of Rs 1500/- . Display the total
income of each salesman along with the sales.
Program:
import java.util.Scanner;
class tut7_5
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int comisn=0,ta=1500;
for(int i=0;i<40;i++)
{
System.out.print("Enter Monthly sale : ");
int sale=s.nextInt();
if(sale>15000) comisn=18;
else if(sale>10000) comisn=15;
else if(sale>5000) comisn=10;
System.out.println("Total income : "+(sale+(comisn*sale/100)+ta));
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 25
Introduction to Software Development
}
}
}
Output
Tutorial – 4,5,6,7
Asia Pacific Institute of Information Technology
Page 26
Download