Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
129 views

Project Java

This document contains code snippets for several Java programs: 1) A time converter that takes user input in days and converts it to seconds. 2) A loan calculator that takes a loan amount as input and deducts 10% over 3 iterations to calculate the final amount. 3) A string reverse program that takes a string as input and reverses it. 4) A binary converter that takes an integer as input and converts it to binary. 5) A shapes program with Square and Circle classes that calculate and print the areas. 6) A bowling game program that tracks scores for 3 players and prints the winner.

Uploaded by

Date Masamune
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

Project Java

This document contains code snippets for several Java programs: 1) A time converter that takes user input in days and converts it to seconds. 2) A loan calculator that takes a loan amount as input and deducts 10% over 3 iterations to calculate the final amount. 3) A string reverse program that takes a string as input and reverses it. 4) A binary converter that takes an integer as input and converts it to binary. 5) A shapes program with Square and Circle classes that calculate and print the areas. 6) A bowling game program that tracks scores for 3 players and prints the winner.

Uploaded by

Date Masamune
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Time Converter

import java.util.Scanner;

public class Program {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int days = scanner.nextInt();

int daysToSeconds = days * 24 * 60 *60;


System.out.println(daysToSeconds);

}
}

Loan Calculator

import java.util.Scanner;

public class Program


{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int amount = scanner.nextInt();
for (int x = 0; x <3; x++){
int actual_amount = (amount * 10)/100;
amount = amount - actual_amount;

System.out.println(amount);

}
}

Reverse a String

import java.util.Scanner;

public class Program


{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
char[] arr = text.toCharArray();
int n=arr.length;
String str="";
for(int i=n-1 ; i>-1 ; i--){
str=str+arr[i];
}
System.out.println(str);
}
}
Binary Converter

import java.util.Scanner;

public class Converter {


public static String toBinary(int num) {
String binary="";
while(num > 0) {
binary = (num%2)+binary;
num /= 2;
}
return binary;
}
}

public class Program {


public static void main(String[ ] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
System.out.print(Converter.toBinary(x));
}
}

Shapes

import java.util.Scanner;

abstract class Shape {


int width;
abstract void area();
}
class Square extends Shape {
public Square(int w){
width = w;
}
public void area(){
width = width*width;
System.out.println(width);
}
}

class Circle extends Shape {


public Circle(int w){
width = w;
}
public void area() {
double areaCircle = (double)Math.PI*(int)width*(int)width;
System.out.println(areaCircle);
}
}

public class Program {


public static void main(String[ ] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();

Square a = new Square(x);


Circle b = new Circle(y);
a.area();
b.area();
}
}

Bowling Game

import java.util.*;

public class Bowling {


HashMap<String, Integer> players;
Bowling() {
players = new HashMap<String, Integer>();
}
public void addPlayer(String name, int p) {
players.put(name, p);
}
public void getWinner(){
String best="";
Iterator<Map.Entry<String,Integer>> it =players.entrySet().iterator();
int max=0;
while(it.hasNext()){

String playerName=it.next().getKey();
Integer checkVal=players.get(playerName);
if (checkVal>=max){

max=checkVal;
best=playerName;
}

System.out.println(best);
}

public class Program {


public static void main(String[ ] args) {
Bowling game = new Bowling();
Scanner sc = new Scanner(System.in);

for(int i=0;i<3;i++) {
String input = sc.nextLine();
String[] values = input.split(" ");
String name = values[0];
int points = Integer.parseInt(values[1]);
game.addPlayer(name, points);
}
game.getWinner();
}
}

You might also like