0% found this document useful (0 votes)
10 views9 pages

CSE23355 Labsheet 3

Uploaded by

messiachu92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

CSE23355 Labsheet 3

Uploaded by

messiachu92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LABSHEET 3

SHIVANAND R
AM.SC.U4CSE23355
Exercise-II
package LABSHEET3;
import java.util.Scanner;
public class TextBook {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.print("Enter no.of books to input ");
int n=sc.nextInt();

Book b[]=new Book[n];


for (int i=0;i<n;i++) {
Scanner sc1=new Scanner(System.in);

b[i]=new Book();
System.out.println("Enter title of the book "+(i+1));
String t=sc1.nextLine();
b[i].setTitle(t);
System.out.println("Enter author name ");
String a=sc1.nextLine();
b[i].setAuthor(a);
System.out.println("Enter ISBN ");
String I=sc1.nextLine();
b[i].setIsbn(I);
System.out.println("Enter price of the book ");
double p=sc1.nextDouble();
b[i].setPrice(p);
}
System.out.println("Details of Books ");
System.out.println("-----------------");

for (int j=0;j<n;j++) {


System.out.println("Book "+(j+1));
System.out.println("-------------");
System.out.println("Title : "+b[j].getTitle());
System.out.println("Author : "+b[j].getAuthor());
System.out.println("ISBN : "+b[j].getIsbn());
System.out.println("Price "+b[j].getPrice()+"/-");
System.out.println(" ");
}
double sum=0;
for(int x=0;x<n;x++) {
sum=sum+b[x].getPrice();
}
double avg=b[1].average(sum,n);
System.out.println("Average Price = "+avg);
}

}
class Book{
private String title,author,isbn;
private double price;

public void setTitle(String t) {


title=t;
}
public String getTitle() {
return title;
}
public void setAuthor(String a) {
author=a;
}
public String getAuthor() {
return author;
}
public void setIsbn(String i) {
isbn=i;
}
public String getIsbn() {
return isbn;
}
public void setPrice(double p) {
price=p;
}
public double getPrice() {
return price;
}
double average(double sum,int n) {
double avg=sum/n;
return avg;
}
}
Output
Sample Programs:
package New;
public class StudentMain {
public static void main(String[] args) {
Student1 s[]=new Student1[3];
for(int i=0;i<3;i++){
s[i]=new Student1();
System.out.println("object created ");
}
}
}
class Student1{
int rollno;
}

Output
package New;

import java.util.Scanner;

public class StudentMain{


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("enter n");
int n=sc.nextInt();
Student s[]=new Student[n];
for(int i=0;i<n;i++){
s[i]=new Student();
System.out.println("object created for "+"student"+(i+1));
System.out.println("enter the rollno and name of student" + (i+1));
s[i].rollno=sc.nextInt();
sc.nextLine();
s[i].name=sc.nextLine();
}
System.out.println("Details are");
for(int i=0;i<n;i++){
s[i].display();
}
}
}
class Student{
int rollno;
String name;
void display(){
System.out.println("Rollno:"+this.rollno);
System.out.println("Name:"+this.name);
}
}

Output

You might also like