Tuesday, May 21, 2013
Object Oriented Programming - Exception handling
Write a java source code of a program with following features:
1) A class StudentNames that manages a String array of names of the 20 students. Include following methods: a. void addName(String name) : add names to the array b. void deleteName(String name) : delete names from the array based on name c. void deleteName(int id) : delete names from the array based on index d. boolean search(String name) : search a name e. void addName(String name, int index): add name to an index
2) Two user defined exceptions IndexNotAvailableException and IndexOutOfRangeException as subclasses of the java Exception class. Throw an IndexNotAvailableException if a name exists in the location. Throw an IndexOutOfRangeException if an index is out of range (index < 0 || index > 20)
public class StudentsName {
String [] names ;
int index;
public StudentsName() {
names = new String[20];
index = 0;
}
void addName(String name) throws IndexOutOfBoundsException{
if(index >= 20){
throw new IndexOutOfBoundsException();
}else{
names[index] = name;
index ++;
}
}
void deleteByName(String name){
for(int i=0;i<20;i++){
if(names[i].equals(name)){
names[i]="";
break;
}
}
}
void deleteByIndex(int id)throws IndexOutOfRange{
if(id <0 || id >=20){
throw new IndexOutOfRange();
}else{
names[id]="";
}
}
boolean search(String name){
for(int i=0;i<20;i++){
if(names[i].equals(name)){
return true;
}
}
return false;
}
void addname(String name , int index) throws IndexOutOfRange, IndexNotAvailable{
if(index < 0 || index >= 0){
throw new IndexOutOfRange();
}else if(!names[index].isEmpty()){
throw new IndexNotAvailable();
}else{
}
}
}
class IndexNotAvailable extends Exception{
public IndexNotAvailable() {
System.out.println("Entered index is not available !!");
}
}
class IndexOutOfRange extends Exception{
public IndexOutOfRange() {
System.out.println("Entered index is not in range !!");
}
}
Sunday, January 20, 2013
Practicle test help
1)Create function to take two integer arguments and compare them and if no1 > no2 then swam them.
3) Create function to read values to array from key board.
void takeInputs(int *array);
4) Create function to dsplay values from array .
void displayValues(int *array);
5) Create the main mehod and do the following.
void swapGreater(int number1, int number2);
2)Create function to take an array as a input argument and shift larger walue in to the last array element.
void arraySort(int *array);
void arraySort(int *array);
first array look - 12, 300 ,4 ,56 , 67, 3
after swap - 12 , 4,56,67,3,300
after swap - 12 , 4,56,67,3,300
3) Create function to read values to array from key board.
void takeInputs(int *array);
4) Create function to dsplay values from array .
void displayValues(int *array);
5) Create the main mehod and do the following.
create array with 6 elements;
take values from keybord using takeInputs function.
take values from keybord using takeInputs function.
do 5 times arraySort function call to the array.
display the array using displayValues function
** this question is 100% not the same , but the program u have to write for this problem is same as the practical test.
ANSWER..............
** this question is 100% not the same , but the program u have to write for this problem is same as the practical test.
ANSWER..............
#include<stdio.h>
void swapGreater(int *no1, int *no2);
void arraySort(int array[]);
void takeInput(int *array);
void display(int *array);
main(){
int array[6],i;
takeInput(array);
printf("before sort !! \n");
display(array);
for(i=0;i<5;i++){
arraySort(array);
}
printf("after sort !! \n");
display(array);
}
void swapGreater(int *no1, int *no2){
int temp;
if(*no1 > *no2){
temp = *no1;
*no1 = *no2;
*no2 = temp;
}
}
void arraySort(int array[]){
int i;
for(i=0;i<5;i++){
swapGreater(&array[i],&array[i+1]);
}
}
void takeInput(int *array){
int i;
printf("enter 6 values \n");
for(i=0;i<6;i++){
scanf("%d",&array[i]);
}
}
void display(int *array){
int i;
for(i=0;i<6;i++){
printf("%d \n",array[i]);
}
}
Thursday, November 29, 2012
Exercises (arrays, pointers, functions).
Exercises (arrays, pointers, functions).
1. Write a program to update the value of the double variable flt_num from 123.45 to 543.21 by
using a double pointer.
2. Given a character variable ch and ch = ‘A’, write a program to update the value of ch to
character 'a' by using a pointer.
3. Given an integer array initialized as (int niceArray[]={23,4,4,56,45,5,6,1,34,787};), write a
program to increase each array element by 1000 using a nother pointer.
4. Write a function to calculate a factorial value. Then use that function to calculate 6C2 (possible
combinations in selecting 2 out of 6) and display the value.
(Hint: int factorial(int f1); combinations=factorial(6)/(factorial(2)*factorial(4)); )
5. Write seperate functions,
1. to read an integer value from the keyboard, (int readNumber(); )
2. to display two integer values, (void display(int a,int b); )
3. to swap two integer values, (void swap(int *c,int *d); )
Write a program to read two integer values to two variables, display them
before their values are swaped between them then swap those values and
finally display the two variables. (Hint: Use above functions)
Thursday, November 22, 2012
Loop Assignment Answers
Loop Assignment Answers
1) Write a program to display numbers from 1 to 250.
2) Write a program to calculate the sum of all the numbers less than a given number n.
ex: output
Enter number : 3
6
Enter number : 5
15
3) Write a program to display “hello world” for every time we enter the character 'y' and stop the
program when we enter the character 'n'. For any other character program should not stop or
display anything.
4) Write a program to calculate the factorial value for a given number N.
5) Write a program to calculate number of possible combinations that we can have when we
selecting r number of elements from n number of different elements. This can be calculated by
calculating the value n!/(r!*(n-r)!).
Thursday, November 15, 2012
Assignment Answers
Assignment Answers
1) Write a program to check the given number is positive or negative.
2) Write a program to Add two numbers if the first one is greater than the second number if not
display an error message.
3) Write a program to display the grade (A, B, C or D) according to the given mark.
hint: A(between 100 and 75)/ B(between 74 and 65)/ C(between 64 and 55)/ D(between 54 and
0)
4) Write a program to read a two characters and display them if both are not equal.
5) Write a program to input a temperature reading in either Celsius (c) or Fahrenheit (f)scale and
convert it to the other scale. The temperature reading consists of a decimal integer followed by
letter “F” or “f” if the temperature is in Fahrenheit scale or letter “C” or “c” if it is in Celsius
scale. You may use a similar format for the output of the program.
Thursday, August 16, 2012
Pointers
Pointers.
A pointer is a variable whose value is an address.
int thing; /* define a thing */
int *thing_ptr; /* define a pointer to a thing */
There are things and pointers to things. Knowing the difference between the two is very important.
The address of thing is 0x1000. Addresses are automatically assigned by the C compiler to every variable. Normally, you don't have to worry about the addresses of variables, but you should understand that they're there.
Our pointer (thing_ptr) points to the variable thing. Pointers are also called address variables because they contain the addresses of other variables. In this case, our pointer contains the address 0x1000.
Pointer Operator Syntax.
The operator ampersand (&) returns the address of a thing which is a pointer. The operator asterisk (*) returns the object to which a pointer points. These operators can easily cause confusion.
Subscribe to:
Posts (Atom)