Ten years ago, girls were so scarce in high school computer science classes that the number of female students taking Advanced Placement tests in that subject could be counted on one hand in nine states. In five others, there were none.

Latino and African American students were also in short supply, a problem that has bedeviled educators for years and hindered efforts to diversify the high-tech workforce.

Now, an expansion of AP computer science classes is helping draw more girls and underrepresented minorities into a field of growing importance for schools, universities and the economy.

Testing totals for female, black and Latino students all doubled in 2017, following the national debut of an AP course in computer science principles. It joined a longer-established AP course focused on the programming language Java.

Racial and gender imbalances persist. But education leaders said the data show a significant advance in a quest to banish the stereotype that computer science is mainly for coding geeks who tend to be white or Asian-American boys.

“We’re trying to diversify a field that for whatever reason has remained not-so for generations,” said David Coleman, president of the College Board, which oversees the AP program. “Really, what this is about is computer science breaking out of its narrow role.”

Coleman acknowledged “there’s more work to do.” About 27 percent of roughly 100,000 AP computer science test-takers last spring were girls. Black students accounted for 5 percent of those tested and Latino students for 15 percent, well below their overall shares of school enrollment.

The quest to broaden the computer science talent pool hinges, in many ways, on stoking the passion of students such as Adesoji Adenusi and Daijah Etienne to explore the power of programming.

The two Maryland teenagers were hunting one recent morning for commands in Java to maneuver a wheeled robot, known as the Finch, through left-hand turns along the edges of a floor mat. Keeping the gizmo on track was not easy.

At the keyboard, Adenusi toyed with various numbers for wheel velocity. “A couple extra zeros never harmed anybody,” he joked.

“It depends,” shot back Etienne as she walked with the balky little robot. “What if you put a couple extra zeros on a check?”

Adenusi, 18, and Etienne, 17, both seniors, are in the Java-centered class called AP Computer Science A at Charles H. Flowers High School in Prince George’s County, Maryland.

Adenusi, who aims to major in computer science in college, said he is drawn to video-game design and has developed an appreciation for the precision and creativity the subject demands. “Everything really in coding is a choice,” he said. “Colors, shapes, sizes — that’s all up to you.”

Etienne, who is considering computer engineering in college, also took AP Computer Science Principles in the last school year. She said the courses have deepened her understanding of the power of software to make objects come to life. “An iPhone, for example,” she said. “A block of metal, in all honesty. But when you add the coding, it becomes something more.”

At Flowers High, 86 students took an AP computer science test in the spring. That was more than triple the total of 26 in 2016. The new principles course fueled the growth. Most of the school’s students are African-American. Nearly half of those enrolled in AP computer science are girls.

The AP program, which enables students to obtain college credit through testing, offers one of the strongest links between high schools and colleges.

For more than 30 years, high schools have offered AP classes in computer science. But about 10 years ago, educators began to worry about participation. Overall numbers were low. About 20,000 students took the computer science tests in 2007, fewer than the totals for AP French or studio art.

A closer look showed even more dismal trends that year: Only about 3,360 female and 1,300 Latino students took the computer tests. The African American total was a mere 734.

Trevor Packer, senior vice president of the College Board and longtime head of the AP program, said annual reports on computer science testing in that era would make him wince. Idaho, for example, counted 25 boys taking the tests in 2007 — and zero girls.

With help from the National Science Foundation, the College Board and computer scientists at various universities fashioned a new course meant to appeal to a broader audience. AP Computer Science Principles, or CSP, launched nationally in fall 2016.

A College Board video promoting the course made explicit appeals to underrepresented students. “A lot of girls are intimidated because they see computers as, like, a ‘guy thing,’?” one girl says in the video. “If more girls were, like, encouraged, then that wouldn’t be an issue anymore.”

Last spring, 92 girls from Idaho took an AP computer science test. Most were in CSP.

Owen Astrachan, a professor of the practice of computer science at Duke University who helped develop the new class, said it is meant to complement Computer Science A.

“In CSA, it’s all programming, all the time,” Astrachan said. “In CSP, programming is part of it, but it’s not the center of it.” Students have more freedom to design their own projects in CSP. They are assessed at the end of the course on a digital portfolio of work — including a task focused on creating a computer program — as well as a multiple-choice test.

Duke gives credit to students who get a top score of 5 on the new exam or scores of 4 or 5 on the original computer science exam, allowing them to place into higher-level courses. “I’m a big fan of trying to empower high schools,” Astrachan said.

Expanding computer science in high schools takes more than adding a new AP course. It also requires investing in teachers, who often are not experts in the field. Course offerings have long been skimpy in many schools. In 2014, The Washington Post found that fewer than one in 10 high school students in the Washington region were taking a computer science course.

College professors, mindful of these issues, aim to help.

David Malan, who teaches a popular introductory course in computer science at Harvard University, said a version of it tailored to the AP CSP curriculum is available online for high school teachers who want to mine it for problem sets and homework assignments. The goal, he said, “is broadening access to and interest in computer science.”