Sum Of Elements of Arrays Based On Certain Condition
Final Lab 2023 Third
Array Code : Code input array of 10 elements and print sum of elements
Sample output :
Array Code : Code input array of 10 elements and print sum of ODD elements
Sample output :
import java.util.Scanner ;
Sum of odd = 194
Sum of even = 1056
Nested Loop
55555
4444
333
22
1
7
7
1
Class Code (Same AS HW 4)
import java.util.Scanner;
class Book {
private int BookNo;
private String BookName;
private double Price;
public Book() {
BookNo = 0;
BookName = null;
Price = 0.0;
}
public Book(int ID, String name, double p) {
BookNo = ID;
BookName = name;
Price = p;
}
public String getBook(int n) {
return BookName;
}
public double getPrice() {
return Price;
}
public void setBook(String n, double p) {
BookName = n;
Price = p;
}
public void printBook() {
System.out.println("======================");
System.out.println("All info of the Book");
System.out.println("======================");
System.out.println("Book Number :" + BookNo);
System.out.println("Book Name :" + BookName);
System.out.println("Book Price :" + Price);
}
public class TestBook {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Book Number : ");
int n = in.nextInt();
in.nextLine();
System.out.print("Enter Book Name : ");
String name = in.nextLine();
System.out.print("Enter Book Price : ");
double p = in.nextDouble();
Book book=new Book(n,name,p);
in.nextLine();
System.out.println();
System.out.print("Enter New Book Name : ");
String newName = in.nextLine();
System.out.print("Enter New Price : ");
Double newPrice = in.nextDouble();
book.setBook(newName, newPrice);
System.out.println();
book.printBook();
}