1. Write a program that prompts the user to enter the number of students, the st
ID: 3715675 • Letter: 1
Question
1. Write a program that prompts the user to enter the number of students, the student’s names, and their scores. The program then prints the student names in decreasing order of their scores.
2. Design a class named rectangle. The class contains:
- Two double data fields named width and length, with default values of 1 for both.
- A no-arg constructor that creates a default rectangle
- A constructor that creates a rectangle with a user-specified width and length
- A method named getArea() that returns the area of the rectangle
- A method named getPerimeter() that returns the perimeter of the rectangle
3. Name and describe the differences between the three visibility modifiers
4. Assume a Student class has two variables: Name and Grade. Write an example of a get method and a set method for each of the two variables
Explanation / Answer
1)
package org.students;
import java.util.Scanner;
public class Demo100 {
public static void main(String[] args)
{
int size=0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.print("How many students data you want to enter :");
size=sc.nextInt();
String names[]=new String[size];
int scores[]=new int[size];
for(int i=0;i<size;i++)
{
sc.nextLine();
//Getting the input entered by the user
System.out.print("Enter name :");
names[i]=sc.nextLine();
System.out.print("Enter Grade :");
scores[i]=sc.nextInt();
}
//This Logic will Sort the Array of elements in Decending order
int temp1;
String ntemp;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (scores[i] < scores[j])
{
temp1 = scores[i];
scores[i] = scores[j];
scores[j] = temp1;
ntemp=names[i];
names[i]=names[j];
names[j]=ntemp;
}
}
}
System.out.println("Displaying Numbers After Sorting Scores in Decending Order :");
for(int i=0;i<size;i++)
{
System.out.println(names[i]+" "+scores[i]);
}
}
}
_________________
output:
How many students data you want to enter :5
Enter name :Kane Williams
Enter Grade :78
Enter name :Sachin Tendulkar
Enter Grade :67
Enter name :Ricky Pointing
Enter Grade :90
Enter name :James Pattinson
Enter Grade :88
Enter name :Pat Cummins
Enter Grade :83
Displaying Numbers After Sorting Scores in Decending Order :
Ricky Pointing 90
James Pattinson 88
Pat Cummins 83
Kane Williams 78
Sachin Tendulkar 67
_________________
2)
Rectangle.java
public class Rectangle {
//Declaring instance variables
private double width;
private double length;
//Parameterized constructor
public Rectangle(double width, double length) {
super();
this.width = width;
this.length = length;
}
//getters and setters
public void setWidth(double width) {
this.width = width;
}
public double getWidth() {
return width;
}
public void setLength(double length) {
this.length = length;
}
public double getLength() {
return length;
}
//Method which calculates the area of the triangle
public double calculateArea() {
return width * length;
}
}
_______________
3)
There are 3 types of access modifiers:
1) Private : If we declare a class variables,methods or constructor declared as private can only be accessed within the declared class itself.We cant access these private variables ,methods or constructors outside the class.
2)Public :If we declare a class variables,methods or constructor declared as public can be accessed from outside the class.
3) Protected:If we declare variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
_________________
4)
public class Student {
//Declaring instance variables
private String name;
private int grade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
}
______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.