x

Define a class student with int id and string name as data members and a method void SetData ( ). Accept and display the data for five students.

1.4k views

2 Answers

Answer :

import java.io.*;

class student

{

int id;

String name;

BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));

void SetData()

{

try

{

System.out.println("enter id and name for student");

 id=Integer.parseInt(br.readLine());

 name=br.readLine();

}

 catch(Exception ex)

 {}

}

void display()

{

 System.out.println("The id is " + id + " and the name is "+ name);

}

public static void main(String are[])

{

 student[] arr;

 arr = new student[5];

 int i;

 for(i=0;i<5;i++)

 {

arr[i] = new student();

 }

 for(i=0;i<5;i++)

 {

arr[i].SetData();

 }

for(i=0;i<5;i++)

 {

arr[i].display();

 }

}

}
Like 0 like

Answer :

import java.io.*;

class student

{

int id;

String name;

BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));

void SetData()

{

try

{

System.out.println("enter id and name for student");

 id=Integer.parseInt(br.readLine());

 name=br.readLine();

}

 catch(Exception ex)

 {}

}

void display()

{

 System.out.println("The id is " + id + " and the name is "+ name);

}

public static void main(String are[])

{

 student[] arr;

 arr = new student[5];

 int i;

 for(i=0;i<5;i++)

 {

arr[i] = new student();

 }

 for(i=0;i<5;i++)

 {

arr[i].SetData();

 }

for(i=0;i<5;i++)

 {

arr[i].display();

 }

}

}
Like 0 like

Please log in or register to answer this question.

Related Questions


Define a class circle having data members pi and radius. Initialize and display values of data members also calculate area of circle and display it.
Last Answer : class abc {  float pi,radius; abc(float p, float r) { pi=p; radius=r; } void area() { float ar=pi*radius*radius; System.out.println("Area="+ar); } void display() { System.out.println("Pi="+pi ... void main(String args[]) { abc a=new abc(3.14f,5.0f); a.display(); a.area(); } }

Show More

1.4k views 1 answer

Random Images

List any four methods of string class and state the use of each.
Last Answer : The java.lang.String class provides a lot of methods to work on string. By the help of these methods, We can perform operations on string such as trimming, concatenating, converting, comparing, replacing ... " System.out.println(s2); Output: Kava is a programming language. Kava is a platform.

Show More

483 views 1 answer

Random Images

Differentiate between String and String Buffer.
Last Answer : String String Buffer c String is a major class String Buffer is a peer class of String Length is fixed (immutable) Length is flexible (mutable)  Contents of object cannot be modified ... new Ex:- String s= abc ; Ex:- StringBuffer s=new StringBuffer ( abc );

Show More

41 views 1 answer

Random Images

Write a C++ program to declare a class ‘College’ with data members as name and college code. Derive a new class ‘student’ from the class college with data members as sname and roll no. Accept and display details of one student with college data.
Last Answer : #include<iostream.h> #include<conio.h> class college { char name[10]; int collegecode; public: void getcollege() { cout<<"Enter college name:"; cin>>name; cout< ... (); s.getstudent(); s.putcollege(); s.putstudent(); getch(); }

Show More

406 views 1 answer

Random Images

Write a program to declare class student having data members name and percentage. Write constructor to initialize these data members. Accept and display this data for one object.
Last Answer : #include<iostream.h> #include<conio.h> #include<string.h> class student { char name[20]; float per; public: student(char n[],float p) { strcpy(name,n); per=p; } ... { student S("Sachin",78.00); clrscr(); S.putdata(); getch(); }

Show More

583 views 1 answer

Random Images

Explain how to pass parameter to an applet ? Write an applet to accept username in the form of parameter and print “Hello ”.
Last Answer : Passing Parameters to Applet User defined parameters can be supplied to an applet using <PARAM ..> tags. PARAM tag names a parameter the Java applet needs to run, and provides a value for that ... } public void paint(Graphics g) { g.drawString(str,10,100); } }

Show More

670 views 1 answer

Random Images

Write a program to create two threads. One thread will display the numbers from 1 to 50 (ascending order) and other thread will display numbers from 50 to 1 (descending order).
Last Answer : class Ascending extends Thread {  public void run() {  for(int i=1; i<=15;i++) {  System.out.println("Ascending Thread : " + i);  } } } class Descending extends Thread { ... a=new Ascending(); a.start(); Descending d=new Descending(); d.start(); } }

Show More

195 views 1 answer

Random Images

Define Class and Object.
Last Answer : Class: A class is a user defined data type which groups data members and its associated functions together.  Object: It is a basic unit of Object Oriented Programming and represents the real life ... . A typical Java program creates many objects, which as you know, interact by invoking methods. 

Show More

19 views 1 answer

Random Images

Define stream class. List its types.
Last Answer : Definition of stream class: An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other ... , Writer. Types of stream classes: i. Byte stream classes ii. Character stream classes.

Show More

166 views 1 answer

Random Images

Consider the following JAVA program: public class First { public static int CBSE (int x) { if (x < 100) x = CBSE (x +10); return (x - 1); } public static void main (String[] args){ System.out.print(First.CBSE(60)); } } What does this program print? (1) 59 (2) 95 (3) 69 (4) 99
Last Answer : (2) 95 

Show More

65 views 1 answer

Random Images

The Servlet Response interface enables a servlet to formulate a response for a client using the method ............... (A) void log(Exception e, String s) (B) void destroy() (C) int getServerPort() (D) void setContextType(String Type)
Last Answer : (D) void setContextType(String Type)

Show More

39 views 1 answer

Random Images

Write a C++ program to declare a class student with members as roll no, name and department. Declare a parameterized constructor with default value for department as ‘CO’ to initialize members of object. Initialize and display data for two students.
Last Answer : write a C plus plus program to declare a class which accept and display student information such as roll number division and percentage use get data and put data with required parameters

Show More

581 views 2 answers

Random Images

Write a C++ program to declare a class COLLEGE with members as college code. Derive a new class as STUDENT with members as studid. Accept and display details of student along with college for one object of student.
Last Answer : #include<iostream.h> #include<conio.h> class COLLEGE { protected: int collegecode; }; class STUDENT:public COLLEGE { int studid; public: void accept() { cout<<"Enter college ... STUDENT s; clrscr(); s.accept(); s.display(); getch(); }

Show More

117 views 1 answer

Random Images

Write a program to create a vector with five elements as (5, 15, 25, 35, 45). Insert new element at 2nd position. Remove 1st and 4th element from vector.
Last Answer : import java.util.*; class VectorDemo { public static void main(String[] args) { Vector v = new Vector(); v.addElement(new Integer(5)); v.addElement(new Integer(15)); v.addElement(new ... .addElement(new Integer(45)); System.out.println("Original array elements are "); for(int i=0;i

Show More

925 views 1 answer

Random Images

Give output for following code: class student { int roll no; char name [14]; } s[6]; void main() { cout<<sizeof(s); }
Last Answer : Considering roll_no(Single variable) the output is: 96 OR Considering roll, no (Two variables) the output is: 108 OR Considering roll no the output is: error – space between roll and no

Show More

217 views 1 answer

Random Images

On successful compilation a file with the class extension is created. a) True b) False
Last Answer : a) True

Show More

23 views 1 answer

Random Images

Differentiate between class and interfaces
Last Answer : Class Interface 1)doesn't Supports multiple inheritance 1) Supports multiple inheritance 2) extend keyword is used to inherit 2) implements keyword is used ... declaration }  7)syntax Inteface Innterfacename { Final Variable declaration, abstract Method declaration }

Show More

18 views 1 answer

Random Images

Explain the following classes. i)Byte stream class ii)Character Stream Class
Last Answer : i)Byte stream class: 1) InputStream and OutputStream are designed for byte streams 2) Use the byte stream classes when working with bytes or other binary objects. 3) Input Stream ... for handling characters in file are FileReader (for reading characters) and FileWriter (for writing characters).

Show More

408 views 1 answer

Random Images

List the methods of File Input Stream Class.
Last Answer : • void close() • int read() • int read(byte[] b) • read(byte[] b, int off, int len) • int available()

Show More

114 views 1 answer

Random Images

Describe the use of any methods of vector class with their syntax. 
Last Answer : boolean add(Object obj)-Appends the specified element to the end of this Vector. Boolean add(int index,Object obj)-Inserts the specified element at the specified position in this Vector. ... vector. void removeAllElements()-Removes all components from this vector and sets its size to zero.

Show More

400 views 1 answer

Random Images

Distinguish between Input stream class and output stream class.
Last Answer : Java I/O (Input and Output) is used to process the input and produce the output. Java uses the concept of a stream to make I/O operation fast. The java.io package contains all ... , Byte Array Output Stream , Filter output Stream, Piped Output Stream, Object Output Stream, DataOutputStream

Show More

274 views 1 answer

Random Images

Write a program to create a Student class having three data members std_rollno, std_name, std_marks, along with two methods set_data() method to assign values in data member and display() ... std_marks. How to handle this problem using the template concept? Assume suitable data wherever require

Show More

19 views 0 answers

Random Images

Describe final variable and final method
Last Answer : Final method: making a method final ensures that the functionality defined in this method will never be altered in any way, ie a final method cannot be overridden. Syntax: final void findAverage() ... on individual objects of the class. Example of declaring final variable: final int size = 100;

Show More

18 views 1 answer

Random Images

Explain the concept of Dynamic method dispatch with suitable example.
Last Answer : Method overriding is one of the ways in which Java supports Runtime Polymorphism. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. When an overridden ... B(); // object of type B C c = new C(); // object of type C 

Show More

190 views 1 answer

Random Images

Differentiate between method overloading and method overriding.
Last Answer : Sr. No. Method overloading Method overriding 1 Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two ... 4 overloading is a compiletime concept. Overriding is a run-time concept

Show More

18 views 1 answer

Random Images

Explain dynamic method dispatch in Java with suitable example.
Last Answer : Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. When an overridden method is called through a superclass reference, Java determines which version ( ... ; // calling C's version of m1() ref.m1(); } }

Show More

134 views 1 answer

Random Images

State use of finalize( ) method with its syntax.
Last Answer : Use of finalize( ): Sometimes an object will need to perform some action when it is destroyed. Eg. If an object holding some non java resources such as file handle or window character font, then before ... method whenever it is about to recycle an object. Syntax: protected void finalize() { }  

Show More

59 views 1 answer

Random Images

Define package. How to create user defined package? Explain with example.
Last Answer : Java provides a mechanism for partitioning the class namespace into more manageable parts. This mechanism is the package. The package is both naming and visibility controlled mechanism. Package can be created by including package as the first ... []) { Box b=new Box(); b.display(); } } 

Show More

113 views 1 answer

Random Images

Define type casting. Explain its types with syntax and example.
Last Answer : 1. The process of converting one data type to another is called casting or type casting. 2. If the two types are compatible, then java will perform the conversion automatically. 3. It ... requires to Narrowing conversion to inform the compiler that you are aware of the possible loss of precision.

Show More

305 views 1 answer

Random Images

Define exception. State built-in exceptions. 
Last Answer : An exception is a problem that arises during the execution of a program. Java exception handling is used to handle error conditions in a program systematically by taking the necessary action ... browser's security setting. StackOverflowException: Caused when the system runs out of stack space.

Show More

50 views 1 answer

Random Images

Define array. List its types. 
Last Answer : An array is a homogeneous data type where it can hold only objects of one data type. Types of Array:  1)One-Dimensional 2)Two-Dimensional 

Show More

22 views 1 answer

Random Images

Define error. List types of error.
Last Answer : Errors are mistakes that can make a program go wrong. Errors may be logical or may be typing mistakes. An error may produce an incorrect output or may terminate the execution of the program ... crash. Errors are broadly classified into two categories:  1. Compile time errors  2. Runtime errors

Show More

12 views 1 answer

Random Images

Define Constructor. List its types. 
Last Answer : Constructor: A constructor is a special member which initializes an object immediately upon creation. It has the same name as class name in which it resides and it is syntactically similar to ... . Types of constructors: 1. Default constructor 2. Parameterized constructor 3. Copy constructor 

Show More

19 views 1 answer

Random Images

Write a program to perform following task (i) Create a text file and store data in it. (ii) Count number of lines and words in that file. 
Last Answer : import java.util.*; import java.io.*; class Model6B { public static void main(String[] args) throws Exception { int lineCount=0, wordCount=0; String line = ""; BufferedReader br1 = new BufferedReader(new ... : " + lineCount);  System.out.println("Number of words is : " + wordCount); } }

Show More

357 views 1 answer

Random Images

Accept data for five students and display it. Write a C++ program to displya sum of array elements of array size n. 
Last Answer : #include<iostream.h> #include<conio.h> void main() { int arr[20],i,n,sum=0; clrscr(); cout<<"\nEnter size of an array:"; cin>>n; cout<<"\nEnter ... ; } cout<<"\nSum of array elements is:"<<sum; getch(); }

Show More

78 views 1 answer

Random Images

Write a C++ program to declare a class ‘Account’ with data members as accno, name and bal. Accept data for eight accounts and display details of accounts having balance less than 10,000.
Last Answer : #include<iostream.h> #include<conio.h> class Account  { long int accno, bal; char name[10]; public: void getdata() { cout<<"\nEnter account number, balance and name "; cin>> ... 0;i<8;i++)  { a[i].putdata();  } getch();  }

Show More

632 views 1 answer

Random Images

Write a C++ program to declare a class ‘circle’ with data members as radius and area. Declare a function getdata to accept radius and putdata to calculate and display area of circle.
Last Answer : #include<iostream.h> #include<conio.h> class circle { float radius,area; public: void getdata() { cout<<"Enter radius:"; cin>>radius; } void putdata() { area=3 ... { circle c; clrscr(); c.getdata(); c.putdata(); getch(); }

Show More

1.3k views 1 answer

Random Images

Write a program to declare a class 'staff' having data members as name and department. Accept this data for 10 staffs and display names of staff that are in 'CO' department.
Last Answer : #include<iostream.h> #include<conio.h> #include<string.h> class staff {  char name[10], dept[10];  public:  void accept() { cout<<"Enter Name and Department:\t"; cin>> ... i=0;i<=10;i++) { s[i].display(); } getch(); }

Show More

487 views 1 answer

Random Images

Implement a program to declare a class city with data members city name and state. Accept and display data for 1 object using pointer to object.
Last Answer : #include<iostream.h> #include<conio.h> class city { char city_name[20],state[20]; public: void accept() { cout<<"\nEnter city data:"; cout<<"\nName:"; ... ); ptr=&c; ptr->accept(); ptr->display(); getch(); }

Show More

209 views 1 answer

Random Images

Write a program in C++ to declare a class „Journal‟ having data members as journal_nm, price, ISSN_No. Accept this data for two objects and display the name of the journal having greater price.
Last Answer : #include<iostream.h> #include<conio.h> class Journal { char journal_nm[20]; int ISSN_No; float price; public: void accept(); void display(Journal); }; void Journal::accept() { ... clrscr(); j1.accept(); j2.accept(); j1.display(j2); getch(); }

Show More

175 views 1 answer

Random Images

Write a program to declare a class ‘book’ containing data members as ‘title’, ‘author-name’, ‘publication’, ‘price’. Accept and display the information for one object using pointer to that object.
Last Answer : #include<iostream.h> #include<conio.h> class book { char author_name[20]; char title[20]; char publication[20]; float price; public: void Accept(); void Display(); }; void ... title \t author_name \t publication \t price\n ; p-> Display(); getch(); }

Show More

3.4k views 1 answer

Random Images

The Java interpreter is used for the execution of the source code. a) True b) False
Last Answer : a) True

Show More

12 views 1 answer

Random Images

Write a program to create two threads one thread will print even no. between 1 to 50 and other will print odd number between 1 to 50.
Last Answer : import java.lang.*; class Even extends Thread { public void run() { try { for(int i=2;i<=50;i=i+2) { System.out.println("\t Even thread :"+i); sleep(500); } } ... public static void main(String args[]) { new Even().start(); new Odd().start(); } }

Show More

1.5k views 1 answer

Random Images

Differentiate between array and vector.
Last Answer : Array Vector 1) An array is a structure that holds multiple values of the same type. 1)The Vector is similar to array holds multiple objects and like an array; it contains components that ... 6) Array is the static memory allocation. 6) Vector is the dynamic memory allocation.

Show More

27 views 1 answer

Random Images

Explain any two logical operator in java with example.
Last Answer : Logical Operators: Logical operators are used when we want to form compound conditions by combining two or more relations. Java has three logical operators as shown in table: Program demonstrating logical Operators public class ... & b = false a || b = true !(a && b) = true

Show More

274 views 1 answer

Random Images

Explain life cycle of thread.
Last Answer : Thread Life Cycle Thread has five different states throughout its life. 1. Newborn State 2. Runnable State 3. Running State 4. Blocked State 5. Dead State Thread should be in ... it reaches to end of the method. The stop method may be used when the premature death is required. 

Show More

130 views 1 answer

Random Images

Explain life cycle of Applet.
Last Answer : When an applet begins, the AWT calls the following methods, in this sequence: 1. init( ) 2. start( ) 3. paint( ) When an applet is terminated, the following sequence of ... ( ) method is called when the environment determines that your applet needs to be removed completely from memory. 

Show More

175 views 1 answer

Random Images

Write syntax and example of : 1) drawRect() 2)drawOval()
Last Answer : 1)drawRect() : drawRect () method display an outlined rectangle. Syntax: void drawRect(int top,int left, int width,int height) The upper-left corner of the Rectangle is at top and left. The ... and height the following program draws several ellipses and circle. Example: g.drawOval(10,10,50,50);

Show More

865 views 1 answer

Random Images

List access specifiers in Java.
Last Answer : 1)public 2)private 3)friendly 4)protected 5)Private Protected

Show More

24 views 1 answer

Random Images

List any four Java API packages.
Last Answer : 1.java.lang 2.java.util 3.java.io 4.java.awt 5.java.net 6.ava.applet

Show More

48 views 1 answer

Random Images
Show More⇦ PreviousNext ⇨