Skip to content

Commit f4de1eb

Browse files
authored
Merge pull request #3872 from vansh-codes/issue3296
Added gfg basic peoblem solutions (issue 3296)
2 parents b64ed2c + 5dd2eaf commit f4de1eb

File tree

7 files changed

+806
-6
lines changed

7 files changed

+806
-6
lines changed

dsa-problems/gfg-problems/basic/0101-0200.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ export const problems = [
5454
},
5555
{
5656
"difficulty": "Basic",
57-
"gfgLink": "https://www.geeksforgeeks.org/problems/maximum-in-struct-array/1",
58-
"solutionLink": "#",
57+
"gfgLink": "https://www.geeksforgeeks.org/problems/maximum-in-struct-array/0",
58+
"solutionLink": "/dsa-solutions/gfg-solutions/Basic/maximum-in-struct-array",
5959
"problemName": "Maximum in Struct Array"
6060
},
6161
{
@@ -199,7 +199,7 @@ export const problems = [
199199
{
200200
"difficulty": "Basic",
201201
"gfgLink": "https://www.geeksforgeeks.org/problems/stack-designer/1",
202-
"solutionLink": "#",
202+
"solutionLink": "/dsa-solutions/gfg-solutions/Basic/stack-designer",
203203
"problemName": "Stack designer"
204204
},
205205
{

dsa-problems/gfg-problems/basic/0201-0300.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ export const problems = [
439439
{
440440
"difficulty": "Basic",
441441
"gfgLink": "https://www.geeksforgeeks.org/problems/parties-and-seats/1",
442-
"solutionLink": "#",
442+
"solutionLink": "/dsa-solutions/gfg-solutions/Basic/parties-and-seats",
443443
"problemName": "Parties and seats"
444444
},
445445
{
@@ -475,7 +475,7 @@ export const problems = [
475475
{
476476
"difficulty": "Basic",
477477
"gfgLink": "https://www.geeksforgeeks.org/problems/celsius-to-fahrenheit-conversion5212/1",
478-
"solutionLink": "#",
478+
"solutionLink": "/dsa-solutions/gfg-solutions/Basic/celsius-to-fahrenheit-conversion",
479479
"problemName": "Celsius to Fahrenheit Conversion"
480480
},
481481
{
@@ -559,7 +559,7 @@ export const problems = [
559559
{
560560
"difficulty": "Basic",
561561
"gfgLink": "https://www.geeksforgeeks.org/problems/c-classes-introduction/1",
562-
"solutionLink": "#",
562+
"solutionLink": "/dsa-solutions/gfg-solutions/Basic/cpp-classes-introduction",
563563
"problemName": "C++ Classes Introduction"
564564
},
565565
{
+239
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
---
2+
id: cpp-classes-introduction
3+
title: C++ Classes Introduction
4+
sidebar_label: 0101 C++ Classes Introduction
5+
6+
tags:
7+
- CPP
8+
- OOP
9+
- Classes
10+
11+
description: "This is a solution to the C++ Classes Introduction problem on GeeksForGeeks."
12+
---
13+
14+
## Problem Description
15+
Create class named `CollegeCourse` with fields courseID, grade, credits, gradePoints and honorPoints. Calculate honorpoints as the product of gradepoints and credits. GradePoints are calculated as (A-10),(B-9),(C-8),(D-7),(E-6) & (F-5).
16+
Class CollegeCourse contains following functions:
17+
1. set_CourseId( string CID): sets courseId
18+
2. set_Grade(char g): sets grade equal to g
19+
3. set_Credit(int cr): sets credits equal to cr
20+
4.calculateGradePoints(char g): returns gradePoint(int)
21+
5. calculateHonorPoints(int gp,int cr): return honorPoint (float)
22+
6. display(): prints gradePoint and honorPoint
23+
24+
### Examples
25+
26+
**Example 1:**
27+
```
28+
Input:
29+
The first line contains an integer T, the number of test cases. For each test case, there is a string CID, denoting Course ID, a character g, denoting the grade and an integer cr, denoting the credits of the course.
30+
31+
Output:
32+
For each test case, the output is the gradePoints & the honorPoints of that course.
33+
```
34+
35+
36+
### Constraints
37+
- `1 ≤ T ≤ 100`
38+
- `0 ≤ CID.length() ≤ 100`
39+
- `'A' <= g <= 'F'`
40+
- `1 <= cr <= 4`
41+
42+
Note: Grades are not case sensitive.
43+
44+
### Example
45+
46+
**Example 1:**
47+
```
48+
Input:
49+
2
50+
CSN-206 A 4
51+
ECE-500 d 3
52+
53+
Output:
54+
10 40
55+
7 21
56+
```
57+
58+
59+
## Solution for C++ Classes Introduction
60+
61+
### Code in Different Languages
62+
63+
<Tabs>
64+
65+
<TabItem value="Python" label="Python">
66+
<SolutionAuthor name="@vansh-codes" />
67+
68+
```python
69+
class CollegeCourse:
70+
def __init__(self):
71+
self.courseID = ""
72+
self.gp = 0
73+
self.grade = ''
74+
self.credits = 0
75+
76+
def set_CourseId(self, courseID):
77+
self.courseID = courseID
78+
79+
def set_Grade(self, grade):
80+
self.grade = grade
81+
82+
def set_Credit(self, credits):
83+
self.credits = credits
84+
85+
def calculateGradePoints(self, grade):
86+
grade = grade.upper()
87+
if grade == 'A':
88+
self.gp = 10
89+
elif grade == 'B':
90+
self.gp = 9
91+
elif grade == 'C':
92+
self.gp = 8
93+
elif grade == 'D':
94+
self.gp = 7
95+
elif grade == 'E':
96+
self.gp = 6
97+
elif grade == 'F':
98+
self.gp = 5
99+
return self.gp
100+
101+
def calculateHonorPoints(self, gp, credits):
102+
return gp * credits
103+
104+
def display(self):
105+
print(self.calculateGradePoints(self.grade), self.calculateHonorPoints(self.gp, self.credits))
106+
107+
# Example usage:
108+
course = CollegeCourse()
109+
course.set_CourseId("CS101")
110+
course.set_Grade("A")
111+
course.set_Credit(4)
112+
course.display() #
113+
```
114+
115+
</TabItem>
116+
117+
<TabItem value="Java" label="Java">
118+
<SolutionAuthor name="@vansh-codes" />
119+
120+
```
121+
public class CollegeCourse {
122+
private String courseID;
123+
private int gp;
124+
private char grade;
125+
private int credits;
126+
127+
public void setCourseId(String courseID) {
128+
this.courseID = courseID;
129+
}
130+
131+
public void setGrade(char grade) {
132+
this.grade = grade;
133+
}
134+
135+
public void setCredit(int credits) {
136+
this.credits = credits;
137+
}
138+
139+
public int calculateGradePoints(char grade) {
140+
grade = Character.toUpperCase(grade);
141+
switch (grade) {
142+
case 'A':
143+
gp = 10;
144+
break;
145+
case 'B':
146+
gp = 9;
147+
break;
148+
case 'C':
149+
gp = 8;
150+
break;
151+
case 'D':
152+
gp = 7;
153+
break;
154+
case 'E':
155+
gp = 6;
156+
break;
157+
case 'F':
158+
gp = 5;
159+
break;
160+
}
161+
return gp;
162+
}
163+
164+
public int calculateHonorPoints(int gp, int credits) {
165+
return gp * credits;
166+
}
167+
168+
public void display() {
169+
System.out.println(calculateGradePoints(grade) + " " + calculateHonorPoints(gp, credits));
170+
}
171+
172+
public static void main(String[] args) {
173+
CollegeCourse course = new CollegeCourse();
174+
course.setCourseId("CS101");
175+
course.setGrade('A');
176+
course.setCredit(4);
177+
course.display(); // Output: 10 40
178+
}
179+
}
180+
```
181+
182+
</TabItem>
183+
184+
<TabItem value="C++" label="C++">
185+
<SolutionAuthor name="@vansh-codes" />
186+
187+
```cpp
188+
class CollegeCourse
189+
{
190+
//your code here
191+
private:
192+
string courseID;
193+
int gp;
194+
char grade;
195+
int credits;
196+
197+
public:
198+
void set_CourseId(string courseID){
199+
this->courseID = courseID;
200+
}
201+
202+
void set_Grade(char grade){
203+
this->grade = grade;
204+
}
205+
206+
void set_Credit(int credits){
207+
this->credits = credits;
208+
}
209+
210+
int calculateGradePoints(char grade){
211+
grade = toupper(grade);
212+
switch(grade){
213+
case 'A': gp = 10; break;
214+
case 'B': gp = 9; break;
215+
case 'C': gp = 8; break;
216+
case 'D': gp = 7; break;
217+
case 'E': gp = 6; break;
218+
case 'F': gp = 5; break;
219+
}
220+
return gp;
221+
}
222+
223+
int calculateHonorPoints(int gp, int credits){
224+
return gp * credits;
225+
}
226+
227+
void display(){
228+
cout<<calculateGradePoints(grade)<<" "<<calculateHonorPoints(gp, credits)<<endl;
229+
}
230+
231+
};
232+
```
233+
234+
</TabItem>
235+
</Tabs>
236+
237+
## References
238+
239+
- **GeekForGeeks Problem**: [C++ Classes Introduction](https://www.geeksforgeeks.org/problems/c-classes-introduction/0)

0 commit comments

Comments
 (0)