Skip to content

Commit 3527c48

Browse files
authored
Adding some more codes
1 parent 2788f8c commit 3527c48

19 files changed

+925
-48
lines changed

GradeBook.java

+81-48
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,105 @@
11

2-
import java.util.Scanner;
32

4-
public class GradeBook
3+
/*
4+
* Write a java app for the following output. the second line
5+
* in the output "CS101 Introduction to Java Programming!" is
6+
* passed from the constructor. take two dimension array to
7+
* print the student numbers and their respective grades. Write
8+
* functions for calculating average marks, displaying the
9+
* lowest and highest grades. Use inheritance if necessary.
10+
*/
11+
12+
13+
// Grade Book Class.
14+
public class GradeBook
515
{
6-
private String courseName;
16+
// Title of the grade book.
17+
private final String title;
718

8-
// Default constructor
9-
public GradeBook()
10-
{
11-
courseName = "";
12-
}
19+
// array for holding students grades.
20+
private int[][] studentsGrades;
1321

14-
// Parametrized constructor.
15-
public GradeBook(String courseName)
16-
{
17-
this.courseName = courseName;
18-
}
22+
// array for holding average of grades.
23+
private final double[] average = new double[10];
1924

20-
// Setter method.
21-
public void setCourseName(String courseName)
25+
// parametrized constructor.
26+
public GradeBook(String title)
2227
{
23-
this.courseName = courseName;
28+
this.title = title;
2429
}
2530

26-
// Getter method.
27-
public String getCourseName()
31+
// method for getting grades input.
32+
private void getGrades()
2833
{
29-
return (this.courseName);
30-
}
34+
/* here I am hardcoded the array. if you want to get input from user you may use loop like this:
35+
// Scanner sc = new Scanner(System.in);
36+
// for (int i = 0; i < 10; i++)
37+
// {
38+
// for (int j = 0; j < 3; j++)
39+
// {
40+
// studentsGrades[i][j] = sc.nextInt();
41+
// }
42+
} */
3143

32-
// Display method.
33-
public void displayMessage()
34-
{
35-
System.out.println("Welcome to grade book for " + getCourseName());
44+
studentsGrades = new int[][]
45+
{
46+
{87, 96, 70},
47+
{68, 87, 90},
48+
{94, 100, 90},
49+
{100, 81, 82},
50+
{83, 65, 85},
51+
{78, 87, 65},
52+
{85, 75, 83},
53+
{91, 94, 100},
54+
{76, 72, 84},
55+
{87, 93, 73}
56+
};
3657
}
3758

38-
// Average finder method
39-
public void determineClassAverage()
59+
// method for calculating the average of mark of student.
60+
private void calculateAverage()
4061
{
41-
var input = new Scanner(System.in);
42-
43-
int total = 0;
44-
int grade = 0;
45-
int gradeCounter = 1;
46-
int average = 0;
47-
48-
while (gradeCounter <= 10)
62+
for (int i = 0; i < studentsGrades.length; i++)
4963
{
50-
System.out.print("Enter grades : ");
51-
grade = input.nextInt();
52-
total += grade;
53-
gradeCounter++;
64+
double avg = 0.0;
65+
for (int j = 0; j < 3; j++)
66+
{
67+
avg += studentsGrades[i][j];
68+
}
69+
avg /= 3;
70+
average[i] = avg;
5471
}
72+
}
5573

56-
input.close();
74+
// Printing the grades on console.
75+
public void printGrades()
76+
{
77+
System.out.println("Welcome to the grade book for");
78+
System.out.println(title);
5779

58-
average = (total / 10);
80+
System.out.println("\nThe grades are : ");
5981

60-
System.out.println("The total of grades are : " + total);
61-
System.out.println("Class average is : " + average);
82+
System.out.printf("%20s %10s %10s %10s \n", "Test1", "Test2", "Test3", "Average");
83+
for (int i = 0; i < studentsGrades.length; i++)
84+
{
85+
System.out.printf("Student %02d", (i + 1));
86+
for (int j = 0; j < 3; j++)
87+
{
88+
System.out.printf("%10s", studentsGrades[i][j]);
89+
}
90+
// precise up-to 2 decimal point
91+
System.out.printf("%12.2f \n", average[i]);
92+
}
6293
}
6394

64-
// Main driven function.
65-
public static void main(String[] args)
95+
public static void main(String[] args)
6696
{
67-
var gradeBook = new GradeBook("CS50");
68-
69-
gradeBook.displayMessage();
70-
gradeBook.determineClassAverage();
97+
// Creating the grade book object.
98+
GradeBook gradeBook = new GradeBook("CS101 Introduction to Java Programming!");
99+
100+
// calling required methods.
101+
gradeBook.getGrades();
102+
gradeBook.calculateAverage();
103+
gradeBook.printGrades();
71104
}
72105
}

Q_1.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
// Muhammad Naveed
3+
4+
// (Largest and Smallest Integers) Write an application that reads five integers
5+
// and determines and prints the largest and smallest integers in the group.
6+
7+
public class Q_1
8+
{
9+
public static void main(String[] args)
10+
{
11+
int[] numbers = new int[] {3, 9, 1, 0, -5};
12+
13+
int smallest = numbers[0];
14+
int largest = numbers[0];
15+
16+
for (int i = 0; i < numbers.length; i++)
17+
{
18+
largest = (numbers[i] > largest) ? numbers[i] : largest;
19+
smallest = (numbers[i] < smallest) ? numbers[i] : smallest;
20+
}
21+
22+
System.out.println(largest);
23+
24+
System.out.println(smallest);
25+
26+
}
27+
}

Q_10.java

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
3+
// Muhammad Naveed
4+
5+
// (Palindromes) A palindrome is a sequence of characters that reads the same
6+
// backward as forward. For example, each of the following five-digit integers
7+
// is a palindrome: 12321, 55555, 45554 and 11611. Write an application that reads
8+
// in a five-digit integer and determines whether it’s a palindrome.
9+
// If the number is not five digits long, display an error message and allow the user
10+
// to enter a new value.
11+
12+
13+
import java.util.Scanner;
14+
15+
public class Q_10
16+
{
17+
private int number = 0;
18+
private final Scanner sc = new Scanner(System.in);
19+
20+
// getting user input method
21+
public void readNumber()
22+
{
23+
System.out.print("Enter the number : ");
24+
number = sc.nextInt();
25+
//sc.close();
26+
validateNumber();
27+
sc.close();
28+
}
29+
30+
// validating user input method
31+
private void validateNumber()
32+
{
33+
// if ((number >= 10000) && (number <= 99999))
34+
// {
35+
// System.out.println(isPalindrome());
36+
// }
37+
// else
38+
// {
39+
// System.out.println("Invalid input! \nPlease try again");
40+
// readNumber();
41+
// }
42+
String str = Integer.toString(number);
43+
if (str.length() == 5)
44+
{
45+
System.out.println(isPalindrome());
46+
}
47+
else
48+
{
49+
System.out.println("Invalid input! \nPlease try again");
50+
readNumber();
51+
}
52+
}
53+
54+
// Palindrome number finder method
55+
private String isPalindrome()
56+
{
57+
int originalNumber = number;
58+
int reverse = 0;
59+
int rem ;
60+
61+
// reversing the integer number like 123 => 321
62+
while(number != 0)
63+
{
64+
rem = number % 10;
65+
reverse = reverse * 10 + rem;
66+
number /= 10;
67+
}
68+
69+
// returning the string you may also change the return type to void
70+
// and print these strings rather than returning.
71+
if (reverse == originalNumber)
72+
{
73+
return "The given number is palindrome number";
74+
}
75+
else
76+
{
77+
return "The given number is not palindrome number";
78+
}
79+
80+
81+
}
82+
83+
// main driven method.
84+
public static void main(String[] args)
85+
{
86+
// creating the object.
87+
Q_10 obj = new Q_10();
88+
89+
obj.readNumber();
90+
}
91+
}

Q_11.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
// Muhammad Naveed
3+
4+
// (Printing the Decimal Equivalent of a Binary Number) Write an application that inputs
5+
// an integer containing only 0s and 1s (i.e., a binary integer) and prints its decimal
6+
// equivalent. [Hint: Use the remainder and division operators to pick off the binary
7+
// number’s digits one at a time, from right to left. In the decimal number system,
8+
// the rightmost digit has a positional value of 1 and the next digit to the left a
9+
// positional value of 10, then 100, then 1000, and so on. The decimal number 234 can be
10+
// interpreted as 4 * 1 + 3 * 10 + 2 * 100. In the binary number system, the rightmost
11+
// digit has a positional value of 1, the next digit to the left a positional value of 2,
12+
// then 4, then 8, and so on. The decimal equivalent of binary 1101 is
13+
// 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or, 13.]
14+
15+
16+
import java.util.Scanner;
17+
18+
public class Q_11
19+
{
20+
public static void main(String[] args)
21+
{
22+
// scanner class object.
23+
Scanner sc = new Scanner(System.in);
24+
25+
// creating variables of type long
26+
long binaryNumber, decimalNumber = 0, multipleOfTwo = 1, remainder;
27+
int count=0;
28+
29+
// taking input
30+
System.out.print("Input a binary number: ");
31+
binaryNumber = sc.nextLong();
32+
33+
sc.close();
34+
35+
// Converting binary -> decimal
36+
while (binaryNumber != 0)
37+
{
38+
// 100
39+
// remainder = binaryNumber % 10; // 1
40+
// decimalNumber = decimalNumber + remainder * multipleOfTwo; // 0 + 0 * 1 => 2
41+
// multipleOfTwo = multipleOfTwo * 2; // 4
42+
// binaryNumber = binaryNumber / 10; // 1
43+
remainder = binaryNumber % 10;
44+
decimalNumber = (long) (decimalNumber + remainder * Math.pow(2, count));
45+
count += 1;
46+
binaryNumber /= 10;
47+
}
48+
// Printing the decimal number
49+
System.out.println("Decimal Number: " + decimalNumber);
50+
}
51+
}

Q_12.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
// Muhammad Naveed
4+
5+
// (Multiples of 2 with an Infinite Loop) Write an application that keeps displaying in
6+
// the command window the multiples of the integer 2—namely, 2, 4, 8, 16, 32, 64,
7+
// and so on. Your loop should not terminate (i.e., it should create an infinite loop).
8+
// What happens when you run this program?
9+
10+
11+
public class Q_12
12+
{
13+
public static void main(String[] args)
14+
{
15+
int number = 1;
16+
// looping through infinite loop
17+
18+
while (true)
19+
{
20+
// creating multiplication of 2 like : 2, 4, 8, 16, 32, ...
21+
number *= 2;
22+
System.out.print(number + ", "); // printing the numbers.
23+
}
24+
25+
// When we run our program it should print the 31 correct values up to 1073741824. Then, our program prints 0 on the console
26+
// for infinite time.
27+
}
28+
}

0 commit comments

Comments
 (0)