Posts

Showing posts with the label examples

Calculating Max Verstappen's total points so far in the 2024 Formula 1 s...

Image

Java: 3 Easy programs for Class and Objects basics in Java.

Image
Programs: // 1.   Create a class Vehicle. //      a.  The class should have two fields: //          i.  no_of_seats and no_of_wheels. //      b.  Create two objects //          i.  Motorcycle and Car for this class. //      c.  Your output should show the descriptions for Car and Motorcycle package A6 ; class Vehicle {     // Initialize attributes:     int no_of_seats ;     int no_of_wheels ;     // Two-argument constructor:     Vehicle ( int seats , int wheels ) {         no_of_seats   = seats ;         no_of_wheels = wheels ;     }     // Displays values of attributes:     String display () {         return "no_of_seats = " + no_of_seats + ", no_of_wheels = " + no_of_wheels + "." ;     }   ...

C: Program That Calculates Pay Based On Salary And Hours.

Image
 https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_4/exercise2_4.c // Exercise 2-4 // Write a program that prompts for the user’s weekly pay in dollars and the // hours worked to be entered through the keyboard as floating-point values. The program // should then calculate and output the average pay per hour in the following form: // Your average hourly pay rate is 7 dollars and 54 cents. #include <stdio.h> double checkInput(double input, double min, double max); int main(int argc, char* argv[]) { double hours = 0, rate = 0; // Get input for hours: printf("Enter number of hours worked: "); while(hours == 0) { scanf(" %lf", &hours); hours = checkInput(hours, 1, 10000); } // Get input for salary: printf("Enter hourly rate: "); while(rate == 0) { scanf(" %lf", &rate); rate = checkInput(rate, 1, 100); } // Calculate pay:...

C: Calculate Order Price Based On User's Input.

Image
 https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_3/exercise2_3.c // Exercise 2-3 // Daniele Grech Pereira 22 November 2022 // You’re selling a product that’s available in two versions: type 1 is a // standard version priced at $3.50, and type 2 is a deluxe version priced at $5.50. // Write a program using only what you’ve learned up to now that prompts for the user to // enter the product type and a quantity, and then calculates and outputs the price for // the quantity entered. #include <stdio.h> #include <ctype.h> // toupper #include <limits.h> // INT_MAX // FIXED PRODUCT PRICES: #define TYPE1 3.5f // Type1 price #define TYPE2 5.5f // Type2 price // A function that returns the calculated the price. double calculatePrice(int input, int quantity); // A function that validates a user's input. int checkInput(int input, int min, int max); // A function that returns the user's validated input. int getInput(char* msg, int min, int ma...

C: Convert Inches To Yards, Feet, And Inches.

Image
This exercise is taken from Chapter 2 of the book,  "Beginning C", 5th edition . This is my solution to the problem. https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_1/exercise2_1.c // Exercise 2-1 // Write a program that prompts the user to enter a distance in inches and // then outputs that distance in yards, feet, and inches. // There are 12 inches in a foot and 3 feet in a yard. #include <stdio.h> int main(int argc, char* argv[]) { // Constants for conversion process: const int inches_per_foot = 12; const int feet_per_yard = 3; // Variables for user input and calculations: int input = 0, inches = 0, yards = 0, feet = 0; // Prompt user for input in inches: printf("Enter number of inches for conversion to yards, feet, inches: "); scanf(" %d", &input); inches = input; // Convert inches into yards, feet, inches: feet = inches / inches_per_foot; yards = feet /...