0% found this document useful (0 votes)
7 views6 pages

Java Beginner Solutions: Core Concepts

Uploaded by

sridevi
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)
7 views6 pages

Java Beginner Solutions: Core Concepts

Uploaded by

sridevi
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

Java Beginner Solutions - Core Concepts

======================================

1. Print Your Details


---------------------
Code:
```java
public class PrintDetails {
public static void main(String[] args) {
String name = "Anjali";
int age = 25;
String city = "Chennai";
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("City: " + city);
}
}
```

2. Swap Two Numbers (without third variable)


-------------------------------------------
Code:
```java
import [Link];
public class Swap {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
a = a + b;
b = a - b;
a = a - b;
[Link]("a=" + a + " b=" + b);
}
}
```

3. Simple Interest Calculator


-----------------------------
Code:
```java
import [Link];
public class SimpleInterest {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
double p = [Link]();
double r = [Link]();
double t = [Link]();
double si = (p * r * t) / 100.0;
[Link]("Simple Interest = " + si);
}
}
```

4. Even or Odd
--------------
Code:
```java
import [Link];
public class EvenOdd {
public static void main(String[] args) {
int n = new [Link]([Link]).nextInt();
[Link](n % 2 == 0 ? "Even" : "Odd");
}
}
```

5. Largest of 3 Numbers
-----------------------
Code:
```java
import [Link];
public class Largest3 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link](), b = [Link](), c = [Link]();
int max = a;
if(b > max) max = b;
if(c > max) max = c;
[Link]("Largest = " + max);
}
}
```

6. Multiplication Table (1 to 10)


---------------------------------
Code:
```java
import [Link];
public class Table {
public static void main(String[] args) {
int n = new [Link]([Link]).nextInt();
for(int i=1;i<=10;i++){
[Link](n + " x " + i + " = " + (n*i));
}
}
}
```

7. Grade Calculator
-------------------
Code:
```java
import [Link];
public class Grade {
public static void main(String[] args) {
int marks = new [Link]([Link]).nextInt();
char grade;
if(marks >= 90) grade = 'A';
else if(marks >= 80) grade = 'B';
else if(marks >= 70) grade = 'C';
else grade = 'D';
[Link]("Grade: " + grade);
}
}
```

8. Count Digits
---------------
Code:
```java
import [Link];
public class CountDigits {
public static void main(String[] args) {
int n = [Link](new [Link]([Link]).nextInt());
int count = 0;
if(n==0) count = 1;
while(n > 0) { n /= 10; count++; }
[Link]("Digits: " + count);
}
}
```

9. Methods - Add Two Numbers


----------------------------
Code:
```java
public class AddMethod {
public static int add(int a, int b) { return a + b; }
public static void main(String[] args) {
[Link](add(5, 7));
}
}
```

10. Is Eligible to Vote


-----------------------
Code:
```java
public class Vote {
public static boolean isEligible(int age) {
return age >= 18;
}
public static void main(String[] args) {
[Link](isEligible(20)); // true
}
}
```

11. Celsius to Fahrenheit


-------------------------
Code:
```java
public class Temp {
public static double cToF(double c) {
return (c * 9/5) + 32;
}
public static void main(String[] args) {
[Link](cToF(0)); // 32.0
}
}
```

12. Factorial using loop


------------------------
Code:
```java
import [Link];
public class Fact {
public static int factorial(int n) {
int res = 1;
for(int i=1;i<=n;i++) res *= i;
return res;
}
public static void main(String[] args) {
[Link](factorial(new [Link]([Link]).nextInt()));
}
}
```

13. Student Class (objects)


---------------------------
Code:
```java
class Student {
String name;
int rollNo;
int marks;
Student(String name, int rollNo, int marks) {
[Link] = name; [Link] = rollNo; [Link] = marks;
}
void print() {
[Link](name + " " + rollNo + " " + marks);
}
public static void main(String[] args) {
Student s = new Student("Arun", 1, 85);
[Link]();
}
}
```

14. Constructors - Book


-----------------------
Code:
```java
class Book {
String title, author;
double price;
Book() { [Link]("Book created"); }
Book(String t, String a, double p) {
title = t; author = a; price = p;
}
void show() { [Link](title + " by " + author + " Rs." + price); }
public static void main(String[] args) {
Book b = new Book("Java Basics", "Ramesh", 299.0);
[Link]();
}
}
```

15. Inheritance - Person/Employee


--------------------------------
Code:
```java
class Person {
String name; int age;
Person(String n, int a) { name=n; age=a; }
}
class Employee extends Person {
double salary;
Employee(String n, int a, double s) { super(n,a); salary=s; }
void show() { [Link](name + " " + age + " " + salary); }
public static void main(String[] args) {
Employee e = new Employee("Kavya", 30, 50000);
[Link]();
}
}
```

16. Polymorphism - Overloading


-----------------------------
Code:
```java
class Calculator {
int add(int a,int b){return a+b;}
double add(double a,double b){return a+b;}
int add(int a,int b,int c){return a+b+c;}
public static void main(String[] args){
Calculator c = new Calculator();
[Link]([Link](1,2));
[Link]([Link](1.5,2.5));
[Link]([Link](1,2,3));
}
}
```

17. Encapsulation - BankAccount


-------------------------------
Code:
```java
class BankAccount {
private String accNo;
private double balance;
public BankAccount(String accNo, double balance) {
[Link] = accNo; [Link] = balance;
}
public double getBalance(){ return balance; }
public void deposit(double amt){ balance += amt; }
public boolean withdraw(double amt){
if(amt <= balance){ balance -= amt; return true; }
return false;
}
}
```

18. Simple ATM Menu (switch)


----------------------------
Code:
```java
import [Link];
public class ATM {
public static void main(String[] args){
Scanner sc = new Scanner([Link]);
double balance = 1000;
while(true){
[Link]("[Link] [Link] [Link] [Link]");
int ch = [Link]();
switch(ch){
case 1: [Link]("Balance: " + balance); break;
case 2: balance += [Link](); break;
case 3:
double w = [Link]();
if(w <= balance) balance -= w;
else [Link]("Insufficient");
break;
case 4: return;
default: [Link]("Invalid");
}
}
}
}
```

(End of solutions - these are simple, ready-to-run Java snippets. Use `javac` and `java` to compi

You might also like